Loading Files using a Loop with a predictable name pattern
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
Share a link to this question
Hie,
I want to load files using automated loop function into my workspace with predictable name pattern
. Currently I am doing manually. Here is the photo attached for reference.
. Currently I am doing manually. Here is the photo attached for reference.1 Comment
Stephen23
on 28 Dec 2022
"Currently I am doing manually."
Numbering variables like that is a sign that you are doing something wrong.
Instead of forcing pseudo-indices into variable names, you should use actual indices.
Accepted Answer
Stephen23
on 28 Dec 2022
P = 'absolute or relative path to where the files are saved';
N = 10;
C = cell(1,N);
for k = 1:N
F = sprintf('IGP_Small_fr%d.mat',k);
C{k} = Feko_load_2DMatrix(fullfile(P,F));
end
A = cat(3,C{:}) % optional
14 Comments
Zah
on 28 Dec 2022
Dear Stephen, Thanks for your kind help. I am new to MATLAB, could you please mention how to write P ?
Zah
on 28 Dec 2022
Thanks the code is working, but its all different 10 values are stored in a single matrix called A. However if you refer to my previous photo, I wanted 10 different file names not a single file with 10 values in it.

"However if you refer to my previous photo, I wanted 10 different file names not a single file with 10 values in it"
I guess when you write "file" you actually mean variable:
- files are data saved on a hard-drive or similar storage device.
- variables are the data in a MATLAB workspace.
Note the your concept of processing lots of numbered variable names is one way that beginners force themselves into writing slow, complex, inefficient, insecure, buggy code that is hard to debug:
The MATLAB documentation specifically advises against your approach, because it is slow and complex.
In contrast the approach I showed you using indexing is simple and efficient. You should use indexing.
@Stephen23 I understtod your concerns, but initially I have to take seperate matrices in order to analyze. Please help if you have some code for my concern
Jan
on 28 Dec 2022
@Zah: Stephen hits the point. Do not use A1, A2, ... but A{1}, A{2}, ... These are also different matrices.
Could you please send me the final code, as I am still not figuring the solution out
Voss
on 28 Dec 2022
@Zah: Since A is a 3D array containing all 10 of your matrices, you would say A(:,:,1) to refer to the first matrix, A(:,:,2) to refer to the second matrix, and so on.
The matrices are also stored in the cell array C, so you could also say C{1}, C{2}, and so on.
Voss
on 28 Dec 2022
I'm afraid I confused you with my comment before: "The matrices are also stored in the cell array C, so you could also say C{1}, C{2}, and so on."
I meant that any time you want to refer to the first matrix, you can say A(:,:,1) or you can say C{1}. Any time you want to refer to the second matrix you can say A(:,:,2) or you can say C{2}. And so on for the rest.
C is a cell array. Each cell of C contains one of your matrices. C{:} refers to all the matrices stored in C, expressed as a comma-separated list, so its usage was correct before in this line:
A = cat(3,C{:});
and that does the same as what you have now (assuming there are 10 files/matrices):
A = cat(3,C{1},C{2},C{3},C{4},C{5},C{6},C{7},C{8},C{9},C{10});
I recommend putting it back how it was:
A = cat(3,C{:});
since that is more concise and more general (i.e., it will work ragardless of how many matrices are in C).
Read about cell arrays:
and/or use the 3D array A, which also contains all 10 matrices.
Voss
on 28 Dec 2022
If you don't want to use the 3D array A, then use the cell array C.
The point is that, with either of those, you will use indexing to access your matrices, rather than creating multiple variables, each containing one matrix.
More Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
Products
See Also
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
