MATLAB Answers

Make a new a directory and save a file in a loop

1 view (last 30 days)
Kevin Burg
Kevin Burg on 23 Jul 2021
Answered: Image Analyst on 23 Jul 2021
I am post-processing 36 different cases. What I want to do is run the script, save the variable of interest, create a new directory with the case number, then save the mat file and associated post-processing files in that new directory.
I am running into a problem with the save command. I am getting that I am passing in too many arguments. I'm not sure how I can specify the variable of interest that I am trying to save otherwise though. Any better ways to do this? Thanks for the help!
folder = 'C:Users\me\Documents'
for ind_bet=1:36
run_analysis(); % run script
save_plot_vars; % script to save plotting variables
fullFileName = sprintf('./plotvars_%d/plotvars_%d.mat', ind_bet);
j{ind_bet} = save(fullFileName, 'plotvars'); % plot vars is the variable I want to save
post() % post processing script
cd folder % cd back to original directory and start over
end

Accepted Answer

Simon Chan
Simon Chan on 23 Jul 2021
use the following for saving data into a file.
save(fullFileName, 'plotvars'); % plot vars is the variable I want to save

More Answers (1)

Image Analyst
Image Analyst on 23 Jul 2021
The save() function does not return anything. So you cannot take it's output (of which there is none) and stuff it into j{ind_bet}. Perhaps you really wanted to save filenames???
j{ind_bet} = fullFileName; % Save this filename into a cell array.
save(fullFileName, 'plotvars'); % Don't accept any output arguments when you call save().

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!