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.
채택된 답변
Stephen23
2022년 12월 28일
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.
@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?
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에 대해 자세히 알아보기
제품
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)

