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

6 views (last 30 days)
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?

Accepted Answer

Jakob B. Nielsen
Jakob B. Nielsen on 15 Jan 2020
Edited: Jakob B. Nielsen on 15 Jan 2020
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 Comments

Sign in to comment.

More Answers (1)

CAM
CAM on 15 Jan 2020
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).

Community Treasure Hunt

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

Start Hunting!