Load sequential numbered files in MatLab on the same script in different subfolders
2 次查看(过去 30 天)
显示 更早的评论
Hello everyone,
Let's imagine I have my MatLab script at the following address: "C:\Users\890384\Desktop\MatLab". Inside this, there are two folders: "Folder1" and "Folder2", for example. Inside of each of them there are, let's say, for simplicity, two different files, but they are called the same, array1.dat and array2.dat. Suppose these arrays are, in Folder1 and Folder2 the following:
% In Folder 1
array1_folder1=[1 2 3 4 5];
array2_folder1=[6 7 8 9 10];
writematrix(array1_folder1','C:\Users\890384\Desktop\MatLab\Folder1\array1.dat','Delimeter','space')
writematrix(array2_folder1','C:\Users\890384\Desktop\MatLab\Folder1\array2.dat','Delimeter','space')
% In Folder 2
array1_folder2=[6 7 8 9 10];
array2_folder2=[1 2 3 4 5];
writematrix(array1_folder2','C:\Users\890384\Desktop\MatLab\Folder2\array1.dat','Delimeter','space')
writematrix(array2_folder2','C:\Users\890384\Desktop\MatLab\Folder2\array2.dat','Delimeter','space')
Now, in the same path, " C:\Users\890384\Desktop\MatLab", I create a script to read these files and create two independent arrays (one for Folder1 and one for Folder 2). This will come in handy when you have, say, 200 files in each of these two folders with sequential numbered names. So this could easily be done for the individual case of, for example, Folder1:
myFolder='C:\Users\890384\Desktop\MatLab\Folder1/array';
numfiles_folder1=2;
for k=1:numfiles_folder1
myfilename_folder1=sprintf('array%d.dat',k);
mydata_folder1(:,k)=importdata(myfilename_folder1);
end
As we can see, here I specify the sequencing of names in MyFolder. But how can I do the same, in the same script, to create mydata_folder2 at the same time, having two different MyFolders?
Thank you for your attention.
0 个评论
回答(1 个)
Rik
2020-9-17
Use mydata_folder{1} instead of mydata_folder1. That way you can use a simple loop instead of having to generate the variable name every time you want to use it.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!