for m = 1:length(lst_region)
out=cellfun(#(x) str2double(x(1:strfind(x,'_')-1)),lst_region(m));
str=[num2str(out(1)) '.mat'];
save ( str ,distance);
end
Error using save
Argument must contain a string. Line 3
I want to save files like '1.mat' '2.mat' etc.. but i have error can you please help me to fix it
If distance is a variable in your workspace, you will have to call save(str, 'distance');. You have to enter the name of the variable, not the variable itself.
Related
I am trying to save part of my workspace in matlab.
lets say .
a=1
b=3
name1='file_a.mat'
save(name1,a)
and got
Error using save
Argument must contain a character vector.
I also tried .
save name1 a
this generated a file named name1 (instead of file_a.mat) .
I am using matlab 2017 on mac .
thank you
The name of the variable you want to save must be passed to save as a string:
a=1
b=3
name1='file_a.mat'
% v-v here
save(name1,'a')
I have different mat files w/ distinct names. So I am using a function whose input is the mat file. I used "varargin" to enable the function to take different files.
function bestfunc(varargin)
data = load(varargin, '-mat');
end
when I try to call the function like
bestfunc('matrix777')
Matlabe comes up w/ this error:
Error using load
Argument must contain a string.
Any ideas?
you have to get the names of the files. You can use dir() to do that.
dir('*.mat') % will return information about all .mat files in the folder
The output is a struct with more information for each file. TO get the names try
names=struct2cell(dir('*.mat'));
names=names(1,:);
now names is a cellarray with the names of all *.mat files of your folder. TO load data from each go for
for i=1:length(names)
bestfun(names{i});
end
Since you're only passing one argument, you don't need varargin, which is used when you may have a variable number of arguments to a function.
Just use a normal variable name like matname:
function bestfunc(matname)
data = load(matname, '-mat');
end
Then call it as you did before:
bestfunc('matrix777')
I want to save the value of a variable from MATLAB command history in a text. I am trying the command:
Save([d:/work/abc.txt], 'z1', '-ASCII');
An error appears
Error: input charecter is not valid in MATLAB environment or expression.
What you are missing are the quotes within the brackets for denoting string.
['string']
You should use save (with lower case for "s").
Also the filename should be defined as a string: enclose it withi two '; also you do not need the [] unless, for example, you want to build a string using a variable and / or any function to create part of the filename (e. g.
['d:/work/abc_' num2str(k) '.txt']
assuming k value is 3) to get d:/work/abc_3.txt
Try change your code to:
save(['d:/work/abc.txt'], 'z1', '-ASCII');
Hope this helps.
Qapla
I have 31 models an I want to save each one in a specific file
this is my matlab function
formatspec='model%d'
for k = 1:length(libsvmFiles)
baseFileName = libsvmFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
[labels train]=libsvmread(fullFileName);
model=svmtrain(labels,train, '-t 2 -h 0');
file=sprintf(formatspec,k);
save file model;
but the problem is only the first file is saved and its name is 'file' tha's mean the value of the variable file is not evaluated
how can I solve this problem ?
As many Matlab functions, save can be used in function form (save(...)) or in command form (save ...). In the command form that you use, all the arguments are interpreted as strings. That means
save file model
is equivalent to
save('file', 'model')
For the second argument that is correct, because you want to refer to the variable with the name "model". For the first argument it is wrong, because you want to refer to the file name contained in the variable file. The correct syntax to use is therefore
save(file, 'model')
You're missing the parens for the save function. The model variable also needs to be listed as a string since you need to tell the save function the name of the variable, not the variable itself. See Matlab's documentation.
save(file, 'model');
Additionally you don't have an end to your for loop shown, which normally would just throw an error -- however later code might cause this loop to instead only run once. Otherwise you should check your libsvmFiles variable as it might be only length 1 or not be an array.
The code is:
filename = sprintf('michael%s.bat',datestr(now));
...
save (filename,vec)
vec is a vector
I'm getting this error:
Error using save
Argument must contain a string.
Error in sumfnc (line 13)
save (filename,vec)
I'm unsure on how filename is not a string.
The problem is not filename, it is vec. With the functional usage of save, you need to do:
save(filename,'vec')
However, since filename will contain a space, you will also need to modify filename. Try:
save(strrep(filename,' ','_'),'vec')
to replace spaces with _.