hi, this is my code. Figures are plotted correctly but only the last " i " image was saved for all iterations. Please advise which part was wrong. thanks.
for m = 1: size (OSI,1)
if OSI (m) > 0.5
for i = 1: size (trial_trace,3)
p(i) = figure (i);
for j = 1:5
subplot(5,1,j)
for k = 1:8
if j ~= 5
plot (trial_stimstarts_reset(j,k):trial_stimends_mod_reset(j,k), trial_trace{j,k,i})
ylabel(sprintf('Trial %d', j))
if j == 1
title (sprintf('ROI %d: OSI %0.2f DSI %0.2f', m, OSI(m), DSI (m)))
end
elseif j == 5
plot (trial_stimstarts_reset(1,k):trial_stimends_mod_reset(1,k), trial_trace_avg{1,k,i})
ylabel('Average')
end
hold on
end
end
end
cd(path_stim_all{itr})
saveas (p(i),strcat('trace_','ROI', '_', num2str(m), '_', 'OSI', '_', num2str (OSI(m)), 'DSI', '_', num2str (DSI(m)), '.jpeg'));
end
end

 Akzeptierte Antwort

Image Analyst
Image Analyst am 2 Sep. 2022

0 Stimmen

saveas() needs to be inside the loop over i. Right now it's between the end of the i loop but before the end of the m loop.

10 Kommentare

HYZ
HYZ am 2 Sep. 2022
I put saveas between the last 'end' and second last 'end'. It still doesn't solve the issue.
Image Analyst
Image Analyst am 2 Sep. 2022
Bearbeitet: Image Analyst am 2 Sep. 2022
Set a breakpoint there. Does the value of p(i) change? Maybe just get rid of that in saveas so that it saves the current figure. Have it print out the filename first.
fileName = sprintf('trace_ROI_%2.2d_OSI_%2.2d DSI_%2.2d.jpg', m, OSI(m), DSI(m));
fprintf('For i=%d and m=%d, Saving "%s".\n', i, m, fileName);
exportgraphics(gcf, fileName);
HYZ
HYZ am 2 Sep. 2022
I ran your code. I got back the below error.
for m = 1: size (OSI,1)
if OSI (m) > 0.5
for i = 1: size (trial_trace,3)
p(i) = figure;
for j = 1:5 % 4trials + average trial
subplot(5,1,j)
for k = 1:8 % direction
if j ~= 5
plot (trial_stimstarts_reset(j,k):trial_stimends_mod_reset(j,k), trial_trace{j,k,i},'-k')
a = trial_stimstarts_reset(1,k) ;
c = min(cell2mat(trial_trace(j,:,i))); d = max(cell2mat(trial_trace(j,:,i)));
patch([a a+10 a+10 a], [c c d d],[.75 .75 .75],'FaceAlpha',0.3, 'Edgecolor', 'none')
ylabel(sprintf('Trial %d', j))
hold on
elseif j == 5
plot (trial_stimstarts_reset(1,k):trial_stimends_mod_reset(1,k), trial_trace_avg{1,k,i},'-k')
a = trial_stimstarts_reset(1,k) ;
c = min(cell2mat(trial_trace_avg(:,:,i))); d = max(cell2mat(trial_trace_avg(:,:,i)));
patch([a a+10 a+10 a], [c c d d],[.75 .75 .75],'FaceAlpha',0.3, 'Edgecolor', 'none')
ylabel('Average')
xlabel('#frames')
hold on
end
hold on
end
end
cd(path_stim_all{itr})
fileName = sprintf('trace_ROI_%2.2d_OSI_%2.2d DSI_%2.2d.jpg', m, OSI(m), DSI(m));
fprintf('For i=%d and m=%d, Saving "%s".\n', i, m, fileName);
exportgraphics(gcf, fileName);
end
end
end
For i=1 and m=1, Saving "trace_ROI_01_OSI_5.25e-01 DSI_5.84e-01.jpg".
For i=2 and m=1, Saving "trace_ROI_01_OSI_5.25e-01 DSI_5.84e-01.jpg".
For i=3 and m=1, Saving "trace_ROI_01_OSI_5.25e-01 DSI_5.84e-01.jpg".
Error using exportgraphics
The value of 'destination' is invalid. Unable to create output file 'trace_ROI_01_OSI_5.25e-01 DSI_5.84e-01.jpg', Permission
denied.
Error in sandbox04 (line 156)
exportgraphics(gcf, fileName);
Notice that all your filenames are the same. Are OSI and DSI floating point numbers or integers? If they are floating point, change it to
fileName = sprintf('trace_ROI_%2.2d_OSI_%.2f DSI_%.2f.jpg', m, OSI(m), DSI(m));
Is the filename supposed to have 'i' encoded into it? Right now the filename does not change with the value of i.
HYZ
HYZ am 2 Sep. 2022
Bearbeitet: HYZ am 2 Sep. 2022
yes. The filename is not supposed to have i and only encoded into m.
It saved all images for i = 8. basically, it saved only the last i for different m. I put the code before the last two ends.
If I don't save images, the displayed matlab images showed the correct figures. only when I tried to save, same images were saved.
end
cd(path_stim_all{itr})
fileName = sprintf('trace_ROI_%0.2f_OSI_%0.2f DSI_%0.2f.jpg', m, OSI(m), DSI(m));
fprintf('For i=%d and m=%d, Saving "%s".\n', i, m, fileName);
exportgraphics(gcf, fileName);
end
end
For i=8 and m=1, Saving "trace_ROI_1.00_OSI_0.52 DSI_0.58.jpg".
For i=8 and m=2, Saving "trace_ROI_2.00_OSI_0.75 DSI_0.16.jpg".
For i=8 and m=3, Saving "trace_ROI_3.00_OSI_0.62 DSI_0.21.jpg".
For i=8 and m=15, Saving "trace_ROI_15.00_OSI_0.56 DSI_0.00.jpg".
For i=8 and m=21, Saving "trace_ROI_21.00_OSI_0.72 DSI_0.02.jpg".
For i=8 and m=29, Saving "trace_ROI_29.00_OSI_0.55 DSI_0.14.jpg".
For i=8 and m=56, Saving "trace_ROI_56.00_OSI_0.51 DSI_0.20.jpg".
For i=8 and m=98, Saving "trace_ROI_98.00_OSI_0.58 DSI_0.17.jpg".
Can you save yoiur variables into a mat file so I can run your program, like
save('yuzhi.mat', 'trial_stimstarts_reset', 'trial_trace', ...
'trial_stimends_mod_reset', 'trial_trace_avg',...
'OSI', 'DSI', 'trial_trace');
and any others that I may have forgotten? Then attach yuzhi.mat with the paperclip icon.
HYZ
HYZ am 2 Sep. 2022
I attached the variables "yuzhi.mat". thanks in advance!
Image Analyst
Image Analyst am 2 Sep. 2022
Please add path_stim_all to the list. It's bombing when it gets to that line.
HYZ
HYZ am 2 Sep. 2022
I reattached the .mat.
HYZ
HYZ am 3 Sep. 2022
Bearbeitet: HYZ am 3 Sep. 2022
path_stim_all{itr} is just for where to save. sorry forgot to mention.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Tags

Gefragt:

HYZ
am 1 Sep. 2022

Bearbeitet:

HYZ
am 3 Sep. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by