only saving last image
显示 更早的评论
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
采纳的回答
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 个评论
I put saveas between the last 'end' and second last 'end'. It still doesn't solve the issue.
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);
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.
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.
I attached the variables "yuzhi.mat". thanks in advance!
Please add path_stim_all to the list. It's bombing when it gets to that line.
I reattached the .mat.
path_stim_all{itr} is just for where to save. sorry forgot to mention.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Interactive Model Editing 的更多信息
标签
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
