Having trouble with saving multiple figures as they are produced in sequence.

Hello. I'm currently having some trouble saving some figures that my script is producing from a pool of pre-collected data. Really I just need the figures in a .fig format so I can inspect them and obtain the data points in a seperate script. My script currently reads:
cycle=[200 80 40 20];
begin=[753 785 795 739];
cnum=[2 4 8 16];
for i=1:4:16
ii=(i-1)/4+1;
subplot(2,2,ii)
for j=0:3
k=i+j;
for ll=1:cnum(FREQ)
tempsin=sintrace(begin(FREQ)+cycle(FREQ)*(ll-1):begin(FREQ)+cycle(FREQ)*ll-1,k);
sinpart(1:cycle(FREQ),ll)=tempsin;
end
clear tempsin;
tempsum=sum(sinpart,2)/cnum(FREQ);
sinavg(1:cycle(FREQ),k)=tempsum;
clear tempsum sinpart;
plot(vsine(begin(FREQ):begin(FREQ)+cycle(FREQ)-1,k),sinavg(1:cycle(FREQ),k))
hold on
end
hold off
title([num2str(freq(FREQ)),'Hz, ','Mean V=',num2str(vmean(ii)),'V'])
axis([-100 100 -1.5 1.5,])
end
pause
plot(vsine(begin(FREQ):begin(FREQ)+cycle(FREQ)-1,k),sinavg(1:cycle(FREQ),k))
fig = gcf;
saveas(fig,'dddd.fig')
With my attempt at saving the figures at the bottom. l honestly feel as though there is a very simple way of going about this that I just havent considered yet.

8 Kommentare

FREQ is not defined. Whereas j is defined but not used.
Is FREQ == j+1?
If you need to get the data points, why not store them while plotting as well?
Also, the use of clear is not needed here, remove those lines.
FREQ is defined in a more superior portion of the script. Here is the full script.
load hyster_20140721_001sin100
load hyster_20140721_001sin250
load hyster_20140721_001sin500
load hyster_20140721_001sin1000
freq=[100 250 500 1000];
cycle=[200 80 40 20];
begin=[1053 1085 1095 1039]-300;
cnum=[2 4 8 16];
for FREQ=1:4
SNUM=16;
for i=1:SNUM
sweepnamestr=sprintf('%s%s%s', 'Trace_',int2str(10-FREQ),'_',int2str(i),'_1_1');
tracetemp=eval(sweepnamestr);
trace(:,i)=tracetemp(:,2)*10^9; %store sweeps in nA
sintrace(:,i)=trace(301:1500,i);
end
time=tracetemp(1:1200,1)*10^3+0.025; %store time in ms, shift by 0.025ms
%for i=1:4
% subplot(2,2,i)
% plot(time,sintrace(:,i:i+3),'.');
% title('File contents')
% xlabel('ms')
% ylabel('nA')
%end
step=0.05;
init=14.175;
inipt=285;
finpt=1501;
ampl=[15 30 45 60];
vmean=[0 -15 -30 -45];
vtime=0:step:1199*step;
counter=1;
for j=1:length(vmean)
for i=1:length(ampl)
for n=1:1200
vsine(n,counter)=ampl(5-i)*sin(2*pi*freq(FREQ)*0.001*vtime(n))+vmean(j);
end
%plot(vtime,vsine, '.')
counter=counter+1;
end
%pause
end
for i=0:3
subplot(2,2,i+1)
plot(time,sintrace(:,1+4*i:4+4*i))
axis([0,60,-1,1.2])
title([num2str(freq(FREQ)),'Hz, ','Mean V=',num2str(vmean(i+1)),'V'])
end
pause
%print('-PHP Officejet 4630')
for i=1:4:16
ii=(i-1)/4+1;
subplot(2,2,ii)
for j=0:3
k=i+j;
plot(vsine(800:1200,k),sintrace(800:1200,k))
hold on
end
hold off
title([num2str(freq(FREQ)),'Hz, ','Mean V=',num2str(vmean(ii)),'V'])
axis([-100 100 -1.5 1.5,])
end
pause
%print('-PHP Officejet 4630')
cycle=[200 80 40 20];
begin=[753 785 795 739];
cnum=[2 4 8 16];
for i=1:4:16
ii=(i-1)/4+1;
subplot(2,2,ii)
for j=0:3
k=i+j;
for ll=1:cnum(FREQ)
tempsin=sintrace(begin(FREQ)+cycle(FREQ)*(ll-1):begin(FREQ)+cycle(FREQ)*ll-1,k);
sinpart(1:cycle(FREQ),ll)=tempsin;
end
clear tempsin;
tempsum=sum(sinpart,2)/cnum(FREQ);
sinavg(1:cycle(FREQ),k)=tempsum;
clear tempsum sinpart;
plot(vsine(begin(FREQ):begin(FREQ)+cycle(FREQ)-1,k),sinavg(1:cycle(FREQ),k))
hold on
end
hold off
title([num2str(freq(FREQ)),'Hz, ','Mean V=',num2str(vmean(ii)),'V'])
axis([-100 100 -1.5 1.5,])
end
pause
plot(vsine(begin(FREQ):begin(FREQ)+cycle(FREQ)-1,k),sinavg(1:cycle(FREQ),k))
fig = gcf;
saveas(fig,'dddd.fig')
I suppose it would be quicker to store while running as well. I tried a few iterations ago but I had some trouble becuase of how the graphs are produced in sequence and not all at once.
@Owen You haven't mentioned what the difficulty is. In what way is your current code malfunctioning?
My code is functioning fine to produce the graphs. I'm just having trouble saving the graphs since they aren't all produced at once. I also need a path to save the data points of the average graphs produced to a text file.
%near the top of your code
PLOTNUM = 0;
Then in the place where you save
PLOTNUM = PLOTNUM + 1;
filename = "dddd" + PLOTNUM + ".fig";
save(fig, filename);
Owen
Owen am 30 Jan. 2024
Bearbeitet: Owen am 30 Jan. 2024
I get a complaint about the arrays having incompatable sizes for the "+" operation. Which doesn't make sense because the arrays should all be 1800x2 doubles. I also get an "unrecognized function or variable 'fig'" error response for the save(fig... function.
Which line is showing the error?
PLOTNUM = PLOTNUM + 1;
filename = "dddd" + PLOTNUM + ".fig";
fig = gcf;
save(fig, filename);
fig = gcf declaration makes it work correctly. Thanks!

Melden Sie sich an, um zu kommentieren.

Antworten (1)

You don't mention the problem, but I will say that I typically put a
drawnow
command just before saving/printing figures. I'm not sure why, but I have found that MATLAB sporadically gets confused about which figure is being saved. Using drawnow, and specifying the figure in the saveas command (as you have already done) generally works for me.

1 Kommentar

Owen
Owen am 29 Jan. 2024
Bearbeitet: Owen am 29 Jan. 2024
This works to save one of my figures (usually the last one produced), but since the figures are produced in sequence and not all at once, I need a path to save all of the average graphs in sequence as they are produced to seperate .fig files. It would also help if I could find a way to save the plot points of the average graphs as they are produced. I guess it would something like (sin100average.fig, sin250average.fig, sin500average.fig, etc...).

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Printing and Saving finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 26 Jan. 2024

Kommentiert:

am 30 Jan. 2024

Community Treasure Hunt

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

Start Hunting!

Translated by