Loading Files using a Loop with a predictable name pattern

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.

1 个评论

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

请先登录,再进行评论。

 采纳的回答

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 个评论

Dear Stephen, Thanks for your kind help. I am new to MATLAB, could you please mention how to write P ?
@Zah: This is simply the folder, which contains the files, e.g.:
P = 'C:\Users\Zah\Documents\myData'
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
@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
@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 shall I write the last line as A = cat(3,C{1},C{2},C{3},C{4}); % optional ?
@Zah: That will make A a 3D array containing just the first four of your matrices. Is that what you want?
@Voss I tried to run the code picture attached. But it shows no other matrices
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 My only concern is to load files using a loop with different names and different matrices. However its coming everything in matrice A which I dont want
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.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Logical 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by