From: Eric on 12 May 2010 13:51 I'm trying to avoid using eval() but am at a loss. My code works fine using eval(), but I'm curious to see if somebody can solve the same problem without it. I have a class called "Wavefront" with a method called "ToMAT". Let W be an instance of a Wavefront object. I want the behavior of >>W.ToMAT('filename.mat') to save the variable W to filename.mat. I want the variable name to be preserved so that >> load('filename.mat') creates a variable called W. The only way I have figured out how to do this is with code that looks like the following (I've trimmed out some error handling and other stuff). This is a member function in Wavefront.m. function [success, filename] = ToMAT(obj, filename) var_name = inputname(1); eval(sprintf('%s = obj;', var_name)); save(filename, var_name); Can somebody solve this problem without using eval? Thanks, Eric
From: us on 12 May 2010 14:13 "Eric " <not(a)givingemail.com> wrote in message <hsepq8$1a7$1(a)fred.mathworks.com>... > I'm trying to avoid using eval() but am at a loss. My code works fine using eval(), but I'm curious to see if somebody can solve the same problem without it. > > I have a class called "Wavefront" with a method called "ToMAT". Let W be an instance of a Wavefront object. I want the behavior of > > >>W.ToMAT('filename.mat') > > to save the variable W to filename.mat. I want the variable name to be preserved so that > > >> load('filename.mat') > > creates a variable called W. The only way I have figured out how to do this is with code that looks like the following (I've trimmed out some error handling and other stuff). This is a member function in Wavefront.m. > > function [success, filename] = ToMAT(obj, filename) > var_name = inputname(1); > eval(sprintf('%s = obj;', var_name)); > save(filename, var_name); > > Can somebody solve this problem without using eval? > > Thanks, > Eric one of the many(!) other solutions function tom(d,fnam) s.(inputname(1))=d; save(fnam,'s'); end us
From: Walter Roberson on 12 May 2010 14:15 Eric wrote: > I have a class called "Wavefront" with a method called "ToMAT". Let W > be an instance of a Wavefront object. I want the behavior of > >>W.ToMAT('filename.mat') > > to save the variable W to filename.mat. I want the variable name to be > preserved so that > > >> load('filename.mat') > > creates a variable called W. The only way I have figured out how to do > this is with code that looks like the following (I've trimmed out some > error handling and other stuff). This is a member function in Wavefront.m. > > function [success, filename] = ToMAT(obj, filename) > var_name = inputname(1); > eval(sprintf('%s = obj;', var_name)); > save(filename, var_name); > > Can somebody solve this problem without using eval? function [success, filename] = ToMat(obj, filename); var_name = inputname(1); SaveStruct.(var_name) = obj; save(filename, '-STRUCT', SaveStruct);
From: Matt J on 12 May 2010 14:17 "Eric " <not(a)givingemail.com> wrote in message <hsepq8$1a7$1(a)fred.mathworks.com>... > I'm trying to avoid using eval() but am at a loss. My code works fine using eval(), but I'm curious to see if somebody can solve the same problem without it. > > I have a class called "Wavefront" with a method called "ToMAT". Let W be an instance of a Wavefront object. I want the behavior of > > >>W.ToMAT('filename.mat') > > to save the variable W to filename.mat. I want the variable name to be preserved so that > > >> load('filename.mat') ============== Note: It is dangerous to create variables using load, and therefore questionable why you would want to do this. The advisable practice is to do S=load('filename'); myVariable=S.whatever; %whatever is the variable name as stored in the file > creates a variable called W. The only way I have figured out how to do this is with code that looks like the following (I've trimmed out some error handling and other stuff). This is a member function in Wavefront.m. > > function [success, filename] = ToMAT(obj, filename) > var_name = inputname(1); > eval(sprintf('%s = obj;', var_name)); > save(filename, var_name); > > Can somebody solve this problem without using eval? ============== The following modification should do it. function [success, filename] = ToMAT(obj, filename) var_name = inputname(1); S.(var_name)=obj; save(filename, '-struct',S);
From: Walter Roberson on 12 May 2010 14:25 us wrote: > function tom(d,fnam) > s.(inputname(1))=d; > save(fnam,'s'); > end That would create a .mat file in which the variable name was 's', rather than being the variable name of the object. Adding the -struct option does the needed conversion work.
|
Next
|
Last
Pages: 1 2 Prev: integers to strings Next: Get file date and time attributes from MATLAB... |