Thread Subject: Change name of variable when it is saved

Subject: Change name of variable when it is saved

From: Thomas Meyer

Date: 12 Feb, 2008 19:58:02

Message: 1 of 4

Hi,

I want to change the name of a variable before it is saved
to a binary .mat file with the SAVE function. That is, if
the user calls my saveAsMatFile(var, filename, varname)
function, I'd like to be able to store the given variable
with the requested name, so that it can be retrieved by that
name later, etc.

I can't figure out how to write the body of this function:
how do I connect my local copy of the passed-in variable
(var) to a variable with the requested name (varnname)? Is
there a way to (de)reference a variable by name string? I
need something like ASSIGN in the current workspace, that
will attach an existing value to a variable that is
referenced by name. But that's not even quite enough.

Maybe there's a smarter way to do this, or more likely, just
a completely obvious one that I've overlooked.

Thanks,

-tom

Subject: Change name of variable when it is saved

From: Arthur G

Date: 12 Feb, 2008 20:16:49

Message: 2 of 4

On Tue, 12 Feb 2008 14:58:02 -0500, Thomas Meyer <tm35@cornell.edu> wrot=
e:
> Hi,
>
> I want to change the name of a variable before it is saved
> to a binary .mat file with the SAVE function. That is, if
> the user calls my saveAsMatFile(var, filename, varname)
> function, I'd like to be able to store the given variable
> with the requested name, so that it can be retrieved by that
> name later, etc.
>
> I can't figure out how to write the body of this function:
> how do I connect my local copy of the passed-in variable
> (var) to a variable with the requested name (varnname)? Is
> there a way to (de)reference a variable by name string? I
> need something like ASSIGN in the current workspace, that
> will attach an existing value to a variable that is
> referenced by name. But that's not even quite enough.
>
> Maybe there's a smarter way to do this, or more likely, just
> a completely obvious one that I've overlooked.
>
> Thanks,
>
> -tom

You *could* use 'eval' to create a variable of the desired name.

However, I think a better solution is to take advantage of
the capability to save structure fields as separate variables
(using "save filename -struct myStruct").

This way, your function is clean and simple:

function saveAsMatFile(var, filename, varname)
% var =3D any variable
% filename =3D valid file name
% varname =3D valid variable name

% you probably want to add some error checking
myStruct =3D struct(varname, var);
save(filename, '-struct', 'myStruct');

--Arthur

Subject: Change name of variable when it is saved

From: Thomas Meyer

Date: 12 Feb, 2008 21:12:01

Message: 3 of 4

"Arthur G" <gorramfreak+news@gmail.com> wrote in message
<op.t6fhabxa1g2ca1@arthurmbp.local>...
>
> However, I think a better solution is to take advantage of
> the capability to save structure fields as separate variables
> (using "save filename -struct myStruct").
>
> --Arthur

Perfect! -- that's exactly what I was looking for. Somehow I
missed the "-struct" option to SAVE when I was perusing the
documentation.

Thanks for the tip,

-tom

(But I wonder: If MatLab can support this level of
introspection internally, why isn't it more readily
available to users? I mean, going through a struct seems a
wee bit of a hack.)

Subject: Change name of variable when it is saved

From: Hosam Aly

Date: 4 May, 2008 08:15:05

Message: 4 of 4

> (I mean, going through a struct seems a
> wee bit of a hack.)

I used to do something similar this way:

function saveAsMatFile(var, filename, varname)
    if isvarname(varname)
        eval([varname '=var;']);
        save(filename, varname);
    else
        error('Invalid variable name.');
    end
end


(This doesn't cost any memory. Also might use inputname(1)
if you want.)

I wonder which one is better? To use eval or a struct?

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com