Load sequential numbered files in MatLab on the same script in different subfolders

조회 수: 2 (최근 30일)
Roderick
Roderick 2020년 9월 17일
답변: Rik 2020년 9월 17일
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.

답변 (1개)

Rik
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.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by