Could someone tell me an easy way to plot graph when kk varies from 1 to 100?

조회 수: 6 (최근 30일)
I run the function file
for kk = 1:4
and saved by using
save(['A', num2str(kk),'.mat']) command and then I plot the graphs as follow
clear
clc
A1 = load('A1.mat');
A2 = load('A2.mat');
A3 = load('A3.mat');
A4 = load('A4.mat');
DAT1 = A1.var_emg_value';
DAT2 = A2.var_emg_value';
DAT3 = A3.var_emg_value';
DAT4 = A4.var_emg_value';
x = A1.Bvalues;
figure()
plot(x,DAT1,'r');
hold on
plot(x,DAT2,'c');
hold on
plot(x,DAT3,'g');
hold on
plot(x,DAT4,'k');
Could someone tell me an easy way to plot graph when kk varies from 1 to 100?

채택된 답변

Jakob B. Nielsen
Jakob B. Nielsen 2020년 1월 15일
편집: Jakob B. Nielsen 2020년 1월 15일
Do you need all the variables open with every information in the workspace after the plotting? If not, I would do something like this. (Note you dont need to hold on after every plot. When you set hold on, hold is on until you tell it to be off.
A1 = load('A1.mat');
x=A1.Bvalues; %am I correct in assuming your x is the same for every run? Otherwise you must define it within the loop
hold on
for j=1:100
runningfilename=strcat('A',num2str(j),'.mat'); %this should give you 'A1.mat' first run, 'A2.mat' 2nd run, etc.
data=load(runningfilename);
yplot=ata.var_emg_value; %if you dont need any matrix/table with data, just the plots.
%%%% OR
%yplot(:,j)=data.var_emg_value; %you will end up with a yplot matrix of dimension ?x100
%and finally plotting:
plot(x,yplot); %OR plot(x,yplot(:,j)); if you go with the matrix option.
end
Try this - it should produce 100 plots in the same window (notice how hold on is only called once). You can do all sorts of clever things with indexing and for loops, once you know how :)
  댓글 수: 5
parag gupta
parag gupta 2020년 1월 15일
Yeah, you are right jakob :)
THANKS ALL :)
parag gupta
parag gupta 2020년 1월 15일
yeah, done Walter.
Thank you so much. :)

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

추가 답변 (1개)

CAM
CAM 2020년 1월 15일
Have you considered putting all emg data columns in a cell array, with the first cell containing the x-values? That way you only have to save one mat-file.
If you want to keep the separate files, you could use a loop to build the cell array as you read each file (and add to the plot).

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by