Line | |
---|
1 | % add an index to a name, or increment an existing index, if the proposed Name (char string) already exists in the list ListName (cell) |
---|
2 | function NewName = rename_indexing(Name,ListName) |
---|
3 | |
---|
4 | detectname=1; |
---|
5 | NewName=Name; |
---|
6 | while ~isempty(detectname) |
---|
7 | detectname=find(strcmp(NewName,ListName),1);%test the existence of the proposed name in the list |
---|
8 | if detectname% if the name already exist |
---|
9 | rr=regexp(NewName,'(\d+)$'); %look for numerical indexing at the end of NewName |
---|
10 | if isempty(rr) |
---|
11 | NewName=[NewName '_1'];%add the index 1 |
---|
12 | else |
---|
13 | newindex=str2num(NewName(rr:end))+1; %increment the index by 1 |
---|
14 | NewName=[NewName(1:rr-1) num2str(newindex)]; |
---|
15 | end |
---|
16 | end |
---|
17 | end |
---|
Note: See
TracBrowser
for help on using the repository browser.