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!