Save Function and uisave are not working properly.

4 views (last 30 days)
Can you please tell me why the following codes are not working?
The functional form of save is not working right, especially when I am trying to specify a name of the file while saving it.
I have tried to extract the filenames from the user defined names using the following code.
Filename = Name(~isspace(Name));
Then I have tried using the following codes to save the file.
Filename = [Filename{:} '.mat'];
uisave({Name, x, y ,z}, Filename);
Another way I tried...
save(Filename, {Name, x, y, z});
The save function is working when the filename specified is static such as
save('all_variables.mat', {Name, x ,y ,z});
Please tell me what am I doing wrong here. What should I do to save the variables along with the user specified dynamic names? And why am I getting an error message in the above pictures? Should it not be saving the variables dynamically instead of showing an error message dialog box?

Answers (1)

dpb
dpb on 4 Jul 2020
Edited: dpb on 5 Jul 2020
Syntax issues...
>> Name='A long File Name'; % just something to follow your lead...
>> Name=Name(~isspace(Name)); % hadn't ever thought of doing this, but does kill embedded blanks
>> dir AlongFileName.* % prove it wasn't already there...
No matches for pattern 'AlongFileName.*'.
% here's the problem -- the variable name must be char() string; you had the variable itself
>> save(Name,'x')
>> dir AlongFileName.* % now there is a file there
AlongFileName.mat
>> whos -file AlongFileName.mat % whos there -- that's the variable from the workspace here...
Name Size Bytes Class Attributes
x 9x3x3 648 double
>>
Similar issues with uisave (altho I've never used it). It is different from save however, in that it does accept/require the cell array of variables to have more than one; save will NOT accept that form--
>> save(Name,{'x','xx'})
Error using save
Must be a string scalar or character vector.
>>
Instead, it uses a list of variable names...
>> whos x*
Name Size Bytes Class Attributes
x 9x3x3 648 double
xx 1x3 24 double
xy 4x5 40 char
>> save(Name,'x','xx')
>> whos -file AlongFileName.mat
Name Size Bytes Class Attributes
x 9x3x3 648 double
xx 1x3 24 double
>>

Categories

Find more on Cell Arrays in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!