using eval with save function

7 views (last 30 days)
Ham Man
Ham Man on 11 Jul 2022
Commented: Image Analyst on 11 Jul 2022
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
Stephen23
Stephen23 on 11 Jul 2022
Edited: Stephen23 on 11 Jul 2022
"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.

Sign in to comment.

Accepted Answer

the cyclist
the cyclist on 11 Jul 2022
It can be tricky to construct the string needed, from a combination of text and numerics. I would recommend a few things here.
The most important one is to not use eval. Instead, use sprintf to create the required strings for the filename and/or any variables you want to save, and then put those strings into the save command itself.
Second, my advice would be to always display the string itself to the command line, so that you can see what it looks like. In your case, it looks like this:
% I defined a bunch of these variables arbitrarily, because you had not
% defined them in the code you posted
try_num = 1;
thresh = 2;
pln_num = 3;
Xl = 4;
Xr = 5;
pathdatasave = (['E:\abc\try' num2str(try_num) '\']);
savestring = ['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) ');' ]
savestring = 'save([pathdatasave loc_vel_thresh2_pln3_try1_between two planes x1=4-x2=5_myfile.mat],x_end_pln3_try1);'
Notice how the displayed string is using the literal string 'pathdatasave' instead of the actual string you wanted to swap in? I think that is the main technical issue.
So, using a simpler example case, I would do something like
try_num = 1;
pln_num = 2;
filename = sprintf('E:\\abc\\try%d\\',try_num)
filename = 'E:\abc\try1\'
varname = sprintf('blah_pln%d',pln_num)
varname = 'blah_pln2'
and then just save directly without using eval:
save(filename,varname)
  3 Comments
the cyclist
the cyclist on 11 Jul 2022
What @Stephen23 says is accurate. Probably the first thing I should have posted is this tutorial about why dynamic variable names are usually a terrible idea.

Sign in to comment.

More Answers (1)

Image Analyst
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
Image Analyst
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!