I use MATLAB 2011. Related to the questions I asked yesterday. How to save plots and work space to a folder automatically that come in a loop.

I have about 900 data sets to be analysed and that can be done with single program. Using a loop for d=1:900 I'm able to do that. for d=1 the first data set is analysed and I get about 5 plots and a work space. Similarly for d=2 till d=900. I want to know if there is a code which saves the plots and work spaces to a folder after executing the program for each data set i.e., for each d=integer a separate folder having corresponding plots and work spaces. If it is possible also suggest me a way to name those folders differently so that they don't get replaced because of having the same name.

 채택된 답변

for i = 1:n
... code
filename = [somepath '\' somefilename num2str(i) '.png'];
saveas(figure,filename)
filename = [somepath '\' somefilename num2str(i) '.mat'];
save(filename)
end

댓글 수: 4

Thank you for your answer. Just for trial I ran the loop for only 2 data sets. There is a problem. The figures with plots are being displayed(not saved) but empty figure files are getting saved. I'm not able to find out why it is so. Can you please help me? This is how I'm doing it.
h = plot( fitresult{1}, xData, yData );
legend( h, 'Heights1 vs. Centers1 with Weights1', 'N vs (J-H) Mag-1', 'Location', 'NorthEast' );
xlabel( 'Centers1' );
ylabel( 'Heights1' );
grid on
filename = ['C:\Users\SRINATH KOMPELLA\Documents\MATLAB\N vs (J-H) Mag-1\SingleGaussian',num2str(d),'.fig'];
saveas(figure,filename)
Thank you for your answer. Just for trial I ran the loop for only 2 data sets. There is a problem. The figures with plots are being displayed(not saved) but empty figure files are getting saved. I'm not able to find out why it is so. Can you please help me? This is how I'm doing it.
h = plot( fitresult{1}, xData, yData );
legend( h, 'Heights1 vs. Centers1 with Weights1', 'N vs (J-H) Mag-1', 'Location', 'NorthEast' );
xlabel( 'Centers1' );
ylabel( 'Heights1' );
grid on
filename = ['C:\Users\SRINATH KOMPELLA\Documents\MATLAB\N vs (J-H) Mag-1\SingleGaussian',num2str(d),'.fig'];
saveas(figure,filename)
My bad - its the figure handle you need to give to saveas
fig_no = figure;
h = plot(...
saveas(fig_no,filename)
thank you lain. I got it right now. Sorry if I'm troubling you... I have another question. That is just for one plot. But for each value of i I have 5 plots. I tried using same h = plot( fitresult{1}, xData, yData );...but it is not working.This is wat matlab showed. An error.
Operands to the and && operators must be convertible to logical scalar values.
Error in saveas (line 64) while ~isempty(h) && ~isfigure(h)
I tried using h(1),h(2)...h(5). still it didn't work.
Please help me in doing that

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

추가 답변 (1개)

for k = 1:900
filename = strcat('my_fig_',num2str(k));
h = figure; % figure handle
plot(a,b); % your data
print(h, '-dbmp', filename);
end

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

태그

질문:

2013년 6월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by