using eval with save function

조회 수: 20 (최근 30일)
Ham Man
Ham Man 2022년 7월 11일
댓글: Image Analyst 2022년 7월 11일
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
Stephen23
Stephen23 2022년 7월 11일
편집: Stephen23 2022년 7월 11일
"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.

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

채택된 답변

the cyclist
the cyclist 2022년 7월 11일
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
Stephen23
Stephen23 2022년 7월 11일
편집: Stephen23 2022년 7월 11일
"Isn't this due to the single quotation?"
No, it is due to the fact that you are attempting to generate an invalid variable name.
The single quotes are not part of the character vector, they are just an artifact of how it is displayed.
Once we actually create the very badly-named variable blah_pln2 the code works correctly:
varname = 'blah_pln2';
blah_pln2 = 3;
save('test.mat',varname)
whos -file test.mat
Name Size Bytes Class Attributes blah_pln2 1x1 8 double
But clearly, having numbered variable names is a sign that you are doing something wrong.
the cyclist
the cyclist 2022년 7월 11일
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.

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

추가 답변 (1개)

Image Analyst
Image Analyst 2022년 7월 11일
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
Ham Man
Ham Man 2022년 7월 11일
Thank you so much every one
Image Analyst
Image Analyst 2022년 7월 11일
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.

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

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by