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 件のコメント

Stephen23
Stephen23 2022 年 12 月 28 日
"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.

サインインしてコメントする。

 採用された回答

Stephen23
Stephen23 2022 年 12 月 28 日

1 投票

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 件のコメント

Zah
Zah 2022 年 12 月 28 日
Dear Stephen, Thanks for your kind help. I am new to MATLAB, could you please mention how to write P ?
Jan
Jan 2022 年 12 月 28 日
@Zah: This is simply the folder, which contains the files, e.g.:
P = 'C:\Users\Zah\Documents\myData'
Zah
Zah 2022 年 12 月 28 日
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.
Stephen23
Stephen23 2022 年 12 月 28 日
編集済み: Stephen23 2022 年 12 月 29 日
"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.
Zah
Zah 2022 年 12 月 28 日
編集済み: Zah 2022 年 12 月 28 日
@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
Jan 2022 年 12 月 28 日
@Zah: Stephen hits the point. Do not use A1, A2, ... but A{1}, A{2}, ... These are also different matrices.
Zah
Zah 2022 年 12 月 28 日
編集済み: Zah 2022 年 12 月 28 日
Could you please send me the final code, as I am still not figuring the solution out
Voss
Voss 2022 年 12 月 28 日
@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.
Zah
Zah 2022 年 12 月 28 日
@Voss shall I write the last line as A = cat(3,C{1},C{2},C{3},C{4}); % optional ?
Voss
Voss 2022 年 12 月 28 日
@Zah: That will make A a 3D array containing just the first four of your matrices. Is that what you want?
Zah
Zah 2022 年 12 月 28 日
編集済み: Zah 2022 年 12 月 28 日
@Voss I tried to run the code picture attached. But it shows no other matrices
Voss
Voss 2022 年 12 月 28 日
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.
Zah
Zah 2022 年 12 月 28 日
@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
Voss
Voss 2022 年 12 月 28 日
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 件)

カテゴリ

製品

質問済み:

Zah
2022 年 12 月 28 日

編集済み:

2022 年 12 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by