필터 지우기
필터 지우기

ploting a file with variable name

조회 수: 1 (최근 30일)
amir
amir 2011년 3월 3일
Hi,
I have some variables in my workspace: profile_01, profile_02,...,profile_09 they are vectors of let say size 100. now, i need to simply plot them. I would like to plot them in a 'for' loop since they are similar outputs. I have tried this: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% for PRFno=1:Ncomponent figure name= num2str(PRFno, 5); fname = ['profile_0',name] plot(fname(:,:),'-') end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
however I got an error. Could you please help me with this? Cheers,Amir

채택된 답변

Knut
Knut 2011년 3월 3일
If you use the {} Code tags, you will make the code a lot more readable for other users, and increase the chance that someone bothers to reply. I assume that this is representative for what you are doing and how MATLAB is responding?
profile_01 = zeros(100,1);
profile_02 = ones(100,1);
Ncomponent = 2;
for PRFno=1:Ncomponent
figure
name = num2str(PRFno, 5);
fname = ['profile_0',name]
plot(fname(:,:),'-')
end
>>
??? Error using ==> plot
Invalid first data argument
A possible work-around is to use eval, but I think that it is not the way to do things. Organizing the data in an array or cell array is probably neater. Anyways, here is something that seems to run:
profile_01 = zeros(100,1);
profile_02 = ones(100,1);
Ncomponent = 2;
for PRFno=1:Ncomponent
figure
name = num2str(PRFno, 5);
fname = ['profile_0',name]
eval(['plot(',fname,')'])
end
  댓글 수: 2
amir
amir 2011년 3월 3일
Thanks for note. That is right, I got the error (as you have shown above) saying that:
??? Error using ==> plot
Invalid first data argument
Jan
Jan 2011년 3월 3일
You can at least move the PLOT out of the EVAL:
plot(eval(fname))

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

추가 답변 (1개)

Jan
Jan 2011년 3월 3일
The efficient and clean method would be to use indices as indices, instead of masking the index as part of the name:
profile_{1} = zeros(100,1);
profile_{2} = ones(100,1);
Ncomponent = 2;
for PRFno=1:Ncomponent
figure;
plot(profile_{PRFno});
end
I've used "profile_", because PROFILE is a Matlab command.

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by