i am trying to save .mat file but its not working.

eval(['save D:\F' int2str(I_fno) '\A' int2str(I_D) '_' int2str(I_NP) '_' int2str(s) ' Best_mat;'])
error:
??? Error using ==> save
Unable to write file D:\F2\A240_50_1: No such file or directory.
Error in ==> Run at 53
eval(['save D:\F' int2str(I_fno) '\A' int2str(I_D) '_' int2str(I_NP) '_'
int2str(s) ' Best_mat;'])

1 Kommentar

Stephen23
Stephen23 am 6 Okt. 2015
Bearbeitet: Stephen23 am 6 Okt. 2015
Don't use eval for such trivial code.
eval makes your life much more difficult by hiding all of the useful code hinting, warnings and error messages that you would get is you wrote code properly, without eval.
Although beginners all seem to believe that eval solves every code challenge, it actually just creates many more problems than it solves, which is exactly what you are experiencing now.
Summary: don't use eval.
Read this to know more about why it a poor programming practice:

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Thorsten
Thorsten am 6 Okt. 2015
Bearbeitet: Thorsten am 6 Okt. 2015
You evaluate the string
save D:\F2\A240_50_1 Best_mat;
That is, you try to save the variable 'Best_mat' to the file A240_50_1 in directory D:\F2\
The error tells you that the directory D:\F2\ does not exist.
So you have to create it with
mkdir('D:\F2')
Don't use eval, but
filename = ['D:\F' int2str(I_fno) '\A' int2str(I_D) '_' int2str(I_NP) '_' int2str(s)];
save(filename, 'Best_mat')

8 Kommentare

Image Analyst
Image Analyst am 6 Okt. 2015
Bearbeitet: Image Analyst am 6 Okt. 2015
He did not use fullfile() unfortunately. Best does not have a \ in front of it, so Best_mat is now part of the folder. So there is no base filename or extension, like Best.mat or whatever. The complete string is only a folder name, for example filename = 'D:\F9\A2_3_4 Best_mat' instead of filename ='D:\F9\A2_3_4\Best.mat'.
i tried this code
filename=['D:\F' int2str(I_fno) '\A' int2str(I_D) '_' int2str(I_NP) '_' int2str(s)];
save(filename,' Best_mat');
but i am getting an error
??? Index exceeds matrix dimensions.
Error in ==> Run at 55
save(filename,' Best_mat');
Thorsten
Thorsten am 6 Okt. 2015
Bearbeitet: Thorsten am 6 Okt. 2015
Have you overwritten save?
which save
Also get rid of the blank before Best. Variables in Matlab cannot start with blank. Should read
save(filename, 'Best_mat');
Muhammad Umer
Muhammad Umer am 6 Okt. 2015
Bearbeitet: Muhammad Umer am 6 Okt. 2015
Code:
filename=['D:\F' int2str(I_fno) '\A' int2str(I_D) '_' int2str(I_NP) '_' int2str(s)];
save(filename, 'Best_mat');
Error:
??? Error using ==> save
Unable to write file D:\F2\A240_50_1: No such file or directory.
Error in ==> Run at 55
save(filename,'Best_mat');
What does the following command return?
isdir('D:\F2')
pathname = sprintf('D:\\F%d', I_fno);
filename = fullfile( pathname, sprintf('A%d_%d_%d.mat', I_D, IN_P, s) );
if ~exist(pathname, 'dir')
mkdir(pathname);
end
save(filename, 'Best_mat')
this one is working
filename=['D:\F2\A' int2str(I_D) '_' int2str(I_NP) '_' int2str(s)];
save(filename, 'Best_mat');
I corrected my version with sprintf().

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 6 Okt. 2015

Bearbeitet:

am 6 Okt. 2015

Community Treasure Hunt

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

Start Hunting!

Translated by