Reputation: 101
I have a folder contain numerous subfolder with names that goes as G####### (# is any number between 0-9). I loaded all the subfolders name into a cell called main. For the new folder, if the previous folder start with G then it become I and the ###### behind it woluld also move one place above it, i.e 0->1 4->5 9->0 so on, but I don't know where I coded run, it is getting stuck in the loop.
for i = 3:name_size_main
str = main{i};
size_str = size(str);
j = 1;
while j ~= size_str(2)
if strcmpi(j,str(j))==1
file_name(j)='I';
j = j+1;
elseif strcmpi(1,str(j))==1
file_name(j)='2';
j = j+1;
elseif strcmpi(2,str(j))==1
file_name(j)='3';
j = j+1;
elseif strcmpi(3,str(j))==1
file_name(j)='4';
j = j+1;
elseif strcmpi(4,str(j))==1
file_name(j)='5';
j = j+1;
elseif strcmpi(6,str(j))==1
file_name(j)='7';
j = j+1;
elseif strcmpi(8,str(j))==1
file_name(j)='9';
j = j+1;
elseif strcmpi(9,str(j))==1
file_name(j)='0';
j = j+1;
elseif strcmpi(0,str(j))==1
file_name(j)='1';
j = j+1;
end
end
mkdir(file_Paths_main,file_name);
end
Upvotes: 0
Views: 667
Reputation: 11917
I'm going to answer your question in two parts – first by proposing a simple way of solving your overall problem, then next to examine the problems with your loop.
Solving your problem
A simple way of solving your overall problem is like so:
str = 'G102019'
file_name = char(str + 1)
file_name(str=='9') = '0'
file_name =
H213120
To understand this, note that each character has an associated number - e.g. capital 'A' is represented by number 65:
>> double('A')
ans =
65
This number can be manipulated and converted back to a character using char
:
>> char(65+3)
ans =
D
And also note that the numbers representing characters come in a nice order:
>> char(33:100)
ans =
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcd
...So all your characters in your original string can be 'pushed up one' just by adding 1 to their numerical representation. I need a hack to deal with the case of 9
->0
, but apart from that all should be well.
Your loop problem
The main problem with your loop is that if none of the if
statements ever execute, j
is never incremented so the while
is always met. Move j=j+1
outside of the if statements to ensure it is always incremented.
The reason none of the if
statements are met is because:
a) The first char in str
is not any of the numbers 0-9,
and
b) strcmpi
requires two strings as input, but you are giving a number and a string - i.e.:
>> strcmpi(2, '2')
ans =
0
Upvotes: 1