How can I save figures when created in a loop?

조회 수: 19 (최근 30일)
Nils
Nils 2023년 1월 31일
댓글: Voss 2023년 2월 1일
Hello, I am running my code in a loop for 66 meteorological stations/data sets. In that code, every dataset is evaluated and different calculations are made. Different figures are being created (n = 20). I managed to run the code in a loop, but I would like to save the figures according to their stationname in a different folder. So, 66 folders with each one including 20 figures from the code. I include the way I import and run the loop as well as one example of a figure. The content of the figure is irrelevant.
Let's say the calculations are made in that loop below, how can I save each file in a folder directory, that is based on its stationname?
I am using Matlab 2018b.
% Import Data from .csv
Stationnames = readtable('C:\my\directory\Stationnames.txt','delimiter',',');
Station = table2array(Stationnames);
ST = 1:length(Station);
S = dir(fullfile('C:\my\directory\data','*.csv'));
for k = 1:numel(S)
fnm = fullfile('C:\my\directory\data',S(k).name);
wind = dataset('file',fnm,'delimiter',',','format',['%s' repmat(' %f',1,4)]);
h1 = figure;
MB = bar(Norm.Comnmiss, 'grouped');
title('Count of low Quality Data Points')
ylabel('Count')
xlabel('Period')
grid on
set(gca, 'XTick', 1:4)
xticklabels({'Until 1999','2000-2005','2006-2011','2012-2021'});
saveas(h1,sprintf('C:\my\directory\...\Count_missing_values%d.fig',S(k).name));
end

채택된 답변

Voss
Voss 2023년 1월 31일
This won't work for a couple of reasons:
sprintf('C:\my\directory\...\Count_missing_values%d.fig',S(k).name)
  1. backslash (\) is an escape character for sprintf, i.e., an indicator of a special character such as \n (newline), \r (carriage return), \t (horizontal tab), etc., so you shouldn't use \ in your sprintf call unless you mean to indicate special characters such as the ones mentioned. You could replace all \ with \\ to escape the backslash and print a literal backslash, but an easier way would be to use %s to represent the first part of your file's full-path name (this is shown below).
  2. %d is not an appropriate format for S(k).name. Use %s to print a character array such as a file name.
Therefore, do something like this instead:
filename_prefix = 'C:\my\directory\...\Count_missing_values';
% ...
saveas(h1,sprintf('%s%s.fig',filename_prefix,S(k).name));
Note that that makes the file names all start with "Count_missing_values", which looks like what you're going for. If instead "Count_missing_values" should be the directory that contains the files, put another backslash on the end:
filename_prefix = 'C:\my\directory\...\Count_missing_values\'; % <- ends with \
% ...
saveas(h1,sprintf('%s%s.fig',filename_prefix,S(k).name));
  댓글 수: 2
Nils
Nils 2023년 2월 1일
Thank you, Voss.
Voss
Voss 2023년 2월 1일
You're welcome!

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

추가 답변 (1개)

Bora Eryilmaz
Bora Eryilmaz 2023년 1월 31일
Use the saveas command to store the figure in a folder. If the folder is not there, you can use the mkdir command to create the folder before storing the figure. Alternatively, you can also use the exportgraphics command to store the figure content.

카테고리

Help CenterFile Exchange에서 Printing and Saving에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by