using eval with save function
显示 更早的评论
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 个评论
"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.
采纳的回答
更多回答(1 个)
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
2022-7-11
Image Analyst
2022-7-11
编辑:Image Analyst
2022-7-11
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
2022-7-11
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Variables 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!