using eval with save function
Show older comments
I'm using eval function to save a .mat file and I'm getting error:
I appreciate any hints!
try_num = 1;
pathdatasave = (['E:\abc\try' num2str(try_num) '\']);
eval(['save([pathdatasave loc_vel_thresh' num2str(thresh) '_pln' num2str(pln_num) '_try' num2str(try_num) ...
'_between two planes x1=' num2str(Xl) '-x2=' num2str(Xr)...
'_myfile.mat],x_end_pln' num2str(pln_num) '_try' num2str(try_num) ');' ])
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
1 Comment
"I appreciate any hints!"
Don't use EVAL to call SAVE.
Don't force meta-data into variable names.
The variable name that you are trying to create
x_end_pln' num2str(pln_num) '_try' num2str(try_num)
tells us that you made the mistake of forcing meta-data into your variable names (i.e. pln_num and try_num), which when you try to access dynamically forces you into writing slow, complex, inefficient, obfuscated, insecure, buggy code that is hard to debug:
In short, bad data design leads to bad code, which is what you are finding out now.
Accepted Answer
More Answers (1)
Image Analyst
on 11 Jul 2022
Don't use eval. See the FAQ:
It's simply not necessary.
try_num = 1;
% Construct the folder name.
folder = fullfile('E:\abc\try', num2str(try_num));
if ~isfolder(folder)
mkdir(folder);
end
% Construct the base file name.
baseFileName = sprintf('loc_vel_thresh%d_pln%d_try%d_between two planes x1=%d-x2=%d_myfile.mat',...
thresh, pln_num, Xl, Xr, pln_num)
% Construct the full name.
fullFileName = fullfile(folder, baseFileName)
% Save the mat file.
save(fullFilename, 'x_end_pln', 'pln_num', 'try_num');
4 Comments
Ham Man
on 11 Jul 2022
Image Analyst
on 11 Jul 2022
Edited: Image Analyst
on 11 Jul 2022
Not sure what you mean. Which variables do you actually want to save? Just list them by name in quotes inside the call to save.
Please give an example of what an actual filename would look like with all the numbers encoded into it.
Ham Man
on 11 Jul 2022
Image Analyst
on 11 Jul 2022
So did this sprintf & save solution work for you? If so, could you click the "Accept this answer" link? Thanks in advance. 🙂 Otherwise tell us what's not working yet.
Categories
Find more on Variables 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!