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.
. 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 件のコメント
Zah
2022 年 12 月 28 日
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'
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.

"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
2022 年 12 月 28 日
@Zah: Stephen hits the point. Do not use A1, A2, ... but A{1}, A{2}, ... These are also different matrices.
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.
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
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 件)
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
製品
参考
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- 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)

