Clear Filters
Clear Filters

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

4 views (last 30 days)
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];
% In Folder 2
array1_folder2=[6 7 8 9 10];
array2_folder2=[1 2 3 4 5];
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:
for k=1:numfiles_folder1
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.

Answers (1)

Rik on 17 Sep 2020
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.


Find more on Files and Folders in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!