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')
Related
I am creating a matlab application that is analyzing data on a daily basis.
The data is read in from an csv file using xlsread()
[num, weather, raw]=xlsread('weather.xlsx');
% weather.xlsx is a spreadsheet that holds a list of other files (csv) i
% want to process
for i = 1:length(weather)
fn = [char(weather(i)) '.csv'];
% now read in the weather file, get data from the local weather files
fnOpen = xlsread(fn);
% now process the file to save out the .mat file with the location name
% for example, one file is dallasTX, so I would like that file to be
% saved as dallasTx.mat
% the next is denverCO, and so denverCO.mat, and so on.
% but if I try...
fnSave=[char(weather(i)) '.mat'] ;
save(fnSave, fnOpen) % this doesn't work
% I will be doing quite a bit of processing of the data in another
% application that will open each individual .mat file
end
++++++++++++++
Sorry about not providing the full information.
The error I get when I do the above is:
Error using save
Argument must contain a string.
And Xiangru and Wolfie, the save(fnSave, 'fnOpen') works as you suggested it would. Now I have a dallasTX.mat file, and the variable name inside is fnOpen. I can work with this now.
Thanks for the quick response.
It would be helpful if you provide the error message when it doesn't work.
For this case, I think the problem is the syntax for save. You will need to do:
save(fnSave, 'fnOpen'); % note the quotes
Also, you may use weather{i} instead of char(weather(i)).
From the documentation, when using the command
save(filename, variables)
variables should be as described:
Names of variables to save, specified as one or more character vectors or strings. When using the command form of save, you do not need to enclose the input in single or double quotes. variables can be in one of the following forms.
This means you should use
save(fnSave, 'fnOpen');
Since you want to also use a file name stored in a variable, command syntax isn't ideal as you'd have to use eval. In this case the alternative option would be
eval(['save ', fnSave, ' fnOpen']);
If you had a fixed file name (for future reference), this would be simpler
save C:/User/Docs/MyFile.mat fnOpen
I want to count the number of image in a folder using a GUI created in Matlab guide 2015b.
I have written this code:
Id = 3 (actually the value of id will be given by user at run time)
path =strcat ( ' c:\user\Desktop\New\TrainData\',Id)
path=strcat (path,'\')
d=dir (path)
n=length (d)
It shows the error that dir can not be used for cell input. This code is working when I use command prompt.
It shows error only when I want to use it through GUI. Initially I thought that it's a problem regarding the path.
So I displayed the path but it gave the perfect result.
I am confused. Kindly provide some solutions in Matlab
Instead of strcat you should use fullfile:
path = fullfile('c:\user\Desktop\New\TrainData',num2str(Id))
And be careful with dir, dir also list the subfolder so check that you only take into account the image file:
d = dir(path);
name = d(~[d.isdir]).name
Chances are you're getting your Id variable from a inputdlg or something. It is being read in as a cell array of strings rather than a string. You can check this using iscell:
iscell(Id)
% 1
You don't see any issues until you hit the dir command because strcat is able to handle this just fine but also yields a cell array of strings.
out = strcat('123', {'4'});
class(out)
% cell
If you read your error message thoroughly, the error explicitly states that the input to dir is a cell and not a string. The way to fix this is to first check if Id is a cell array and convert to a string if necessary.
Id = inputdlg('Enter an ID');
% Convert to a string if Id is a cell array
if iscell(Id)
Id = Id{1};
end
% Get a listing of all files/directories
d = dir(fullfile(folder, num2str(I)));
% Get number of files
nFiles = sum(~[d.isdir]);
Also, you don't want to try to concatenate a number with a string (strcat('abc', 1)) because this will convert the number to it's ASCII code. Instead you'll want to use num2str as shown above.
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
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.
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.