필터 지우기
필터 지우기

Indexing a variable

조회 수: 54 (최근 30일)
Ferd
Ferd 2012년 3월 13일
Hi,
I'm looking for some guidance on how you would index variables. I have read a couple of forums but with no clear picture.
I have looped a matrix array (5x1) of a numerical form to obtain a string output of 5 ".mat" files that has data stored in them. What I want to do next is Load these .mat files in a loop and let Matlab iterate it for each file such that I get my plots for each (or string in this case).
A simple example, for loop output --> mat files = A_1, A_2, A_3, A_4, A_5
load A_1, A_2, A_3, A_4, A_5 each time to create the plots. (?) or extend the above for-loop further to load the file each iteration. (?)
Now, I could load each file each time and get the results (load A_1 then load A_2 etc...) But if there could be some sort of a variable counter that would be great (less work... lol). Further, the 1 to 5 numerical value of the mat file was set by me. (If it would help).
Thanks
Ferd

채택된 답변

Jan
Jan 2012년 3월 13일
list = dir('A_*.mat');
Data = cell(1, length(list));
for k = 1:length(list)
matFilename = list(k).name;
matData = load(matFilename);
Data{k} = matData.A;
% Or if the file A_1 contains the matrix A_1:
% [dummy, name] = fileparts(matFilename);
% Data{k} = matData.(name);
% Or:
% Data{k} = matData.(sprintf('A_%d', k));
end
Be sure to read this also: FAQ: How can I create A1, A2, ... and consider the recommendation to avoid hiding an index in the name of a variable.
  댓글 수: 1
Ferd
Ferd 2012년 3월 13일
Hey Simon, thanks man. The logic works!
The A_1 to A_5 are basically different Runs. For convinience I set it up as the type of Run(the alphabet- same Run) and when it was conducted(the number) Hence, the A's and numbers. Yea, just wanted to plot the results for each Run that have the same parameters.
Thanks Ferd

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by