Search code examples
matlabmkdircd

Matlab: How to 'cd' (change directory) to a folder with name as num2str(#number)?


I have a loop and in each iteration I create a directory with the iteration name and I copy some files inside that folder and afterwards I want to cd to that folder, but when I want to cd, I get the error as

"Error using cd Cannot CD to num2str(i) (Name is nonexistent or not a directory)."

How can I fix this issue?

parfor i=1:20000
    iter=num2str(i);
    mkdir(iter)
    copyfile('./mainfolder',iter)
    cd ./num2str(i)
    [pow_maxx,FFee,AA33,BB33,shape] = main(i);
    power_max(i,:)=pow_maxx(1,:);
    Fe(i,:)=FFee;
    A3(i,:)=AA33;
    B3(i,:)=BB33;  
    Shape_all(i,:)=shape(1,:);  
end

Solution

  • in your code cd ./num2str(i), ./num2str(i) is not a string, just use cd ['./',num2str(i)].

    What's more, if you have some string str1='abc' and str2='def', you can use [str1,str2] to concat them.