Clear Filters
Clear Filters

function saveas output format (how to save a plot in for loop and specific location)

2 views (last 30 days)
Hi Everyone,
I'm having difficulties with 'saveas' function. This is the only output format I can find:
I am looking for an example output format for this function for how to add a changing file name (for each for loop iteration) and how to add location (where I want the files to be saved).
My question:
I have the following command (taken from an inside of a for loop):
histogram(log_band_power) %matrix of 114x5 data
title(strcat('logged-data '+ currentSubject + ' parc60')); %plot title changes with each loop; different patient
saveas(hist_pat, '') % hist_pat is the plot I want to save, '' is for the name of the plot; the problem
...where should I add the following to the [saveas (hist_pat, '')] line of code:
changing name and location: (strcat(dataLoc, 'log_check/log_histogram', currentSubject, 'log.svg'),'hist_pat')
I know the layout will be differnet of the line above, but I can't find any examples online on how to structure that line of code.
Any suggestions?
Thank You!

Answers (1)

Vyte Jan
Vyte Jan on 20 Sep 2022
I think I can answer my own question now!
If anyone is interested:
saveas(gca,string(strcat(dataLoc, 'log_check_patient_60/log_histogram/', currentSubject, '.fig')));
dataLoc: the 'root' path of the location where to save th figure
'log_check_patient_60/log_histogram/': the name of the folder (further location specifications, where I want the file to be saved)
currentSubject: the name of the file, which will change with each for loop in my code
'.fig': specification of the figure, can also use .svg for example
Stephen23 on 20 Sep 2022
Rather than squeezing everything onto one line, using two lines makes the code intent much clearer:
fnm = strcat(dataLoc, 'log_check_patient_60/log_histogram/', currentSubject, '.fig')
You should also learn about SPRINTF and FULLFILE.
Vyte Jan
Vyte Jan on 20 Sep 2022
Thank you @Adam Danz & @Stephen23!
I will have a look at SPRINTF and FULLFILE, haven't come across those yet.
Thats a great suggestion @Stephen23, I'll add that to my code.

Sign in to comment.


Find more on Environment and Settings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!