eval function with struct

eval function with struct

Post by Pablo Vasque » Fri, 07 Nov 2003 14:13:32



Hi,

Is there a way to use the struct function inside the eval function.
Since the struct function syntax uses (') and the eval function uses
it to determine the strings. I don't know how to bypass this problem.

Maybe there is another way (then eval function)to create variables
that contain numbers that come from other variables. Here is an
example...

trim=[34 56; 67 54];
mat34_67=struct('x',{0},'y',{0})

As stated above the problem when I use the eval function is the (').
If you know a way to get the syntax right let me know. Thanks for
your help and time.

Pablo,

 
 
 

eval function with struct

Post by Michael Salloke » Fri, 07 Nov 2003 16:53:42



> Hi,

> Is there a way to use the struct function inside the eval
> function.
> Since the struct function syntax uses (') and the eval function
> uses
> it to determine the strings. I don't know how to bypass this
> problem.

> Maybe there is another way (then eval function)to create variables
> that contain numbers that come from other variables. Here is an
> example...

> trim=[34 56; 67 54];
> mat34_67=struct('x',{0},'y',{0})

> As stated above the problem when I use the eval function is the
> (').
> If you know a way to get the syntax right let me know. Thanks for
> your help and time.

> Pablo,

Hello Pablo,

to get a quote in a string use ''.

For example

a=['''']

results in a string containing one quote.

Michael

 
 
 

eval function with struct

Post by Steven Lor » Fri, 07 Nov 2003 22:57:55



> Hi,

> Is there a way to use the struct function inside the eval function.

Yes, but it's a bad idea to use EVAL if you don't have to.  Having things
magically pop into existence can confuse MATLAB when it's trying to execute
your code and can confuse you when you're trying to debug it.  See this
solution on the support website:

http://www.mathworks.com/support/solutions/data/8145.shtml

and question 3.6 in the comp.soft-sys.matlab FAQ.

Quote:> Since the struct function syntax uses (') and the eval function uses
> it to determine the strings. I don't know how to bypass this problem.

> Maybe there is another way (then eval function)to create variables
> that contain numbers that come from other variables. Here is an
> example...

> trim=[34 56; 67 54];
> mat34_67=struct('x',{0},'y',{0})

Yes; try this instead:

mat{trim(1),trim(2)} = struct('x',{0},'y',{0});

The equivalent EVAL statement would be:

eval(sprintf('mat%d_%d = struct(''x'',{0},''y'',{0})',trim(1),trim(2)))

However, if you had a function named mat34_67 on your path, any references
further in your function file to mat34_67 would use that function rather
than this variable, since MATLAB had no clue that this variable would exist
when it was parsing the file.  It may not be an issue now ... but can you
always be sure that there will never be a file with that name on the path?
The cell array statement I first suggested allows MATLAB to determine that
mat is a variable while parsing and so it will correctly handle references
to it later in your code.

--
Steve Lord

 
 
 

eval function with struct

Post by pablo vasque » Sat, 08 Nov 2003 04:50:04


Hi Steven,

When I try the first option it creates a cell array with the name
"mat" with the size 34x67. The second option works great, but I was
wondering if it was possible to do the same thing without the eval
function?



>> Hi,

>> Is there a way to use the struct function inside the eval
> function.

> Yes, but it's a bad idea to use EVAL if you don't have to. Having
> things
> magically pop into existence can confuse MATLAB when it's trying to
> execute
> your code and can confuse you when you're trying to debug it. See
> this
> solution on the support website:

> <http://www.mathworks.com/support/solutions/data/8145.shtml>

> and question 3.6 in the comp.soft-sys.matlab FAQ.

>> Since the struct function syntax uses (') and the eval function
> uses
>> it to determine the strings. I don't know how to bypass this
> problem.

>> Maybe there is another way (then eval function)to create
> variables
>> that contain numbers that come from other variables. Here is an
>> example...

>> trim=[34 56; 67 54];
>> mat34_67=struct('x',{0},'y',{0})

> Yes; try this instead:

> mat{trim(1),trim(2)} = struct('x',{0},'y',{0});

> The equivalent EVAL statement would be:

> eval(sprintf('mat%d_%d =
> struct(''x'',{0},''y'',{0})',trim(1),trim(2)))

> However, if you had a function named mat34_67 on your path, any
> references
> further in your function file to mat34_67 would use that function
> rather
> than this variable, since MATLAB had no clue that this variable
> would exist
> when it was parsing the file. It may not be an issue now ... but
> can you
> always be sure that there will never be a file with that name on
> the path?
> The cell array statement I first suggested allows MATLAB to
> determine that
> mat is a variable while parsing and so it will correctly handle
> references
> to it later in your code.

> --
> Steve Lord


 
 
 

1. Vectorization/de-eval/Array of strucutres 2 struct of arrays

% How can I do this w/o loops or eval?

function z=aos2soa(t)
% AOS2SOA convert an array of structures (slow) to
% a structure of arrays (fast)
%
% clear a,b;
% a(1).b=1;
% a(2).b=3;
% a(1).c='abc';
% a(2).c='def';
% a(3).c='ghi';
% a(1).x=1.01;
% a(2).x=1.02;
% a(3).x=1.03;
% b=aos2soa(a)

z=[];
subfields=subfieldnames(t);
j=min(findstr(char(subfields(1)),'.'))+1;
for i=1:length(subfields)
   subfieldname=char(subfields(i));
   tempstr=['z.' subfieldname(j:end) '=' ...
      '[' subfieldname ']'';'];
   eval(['sfn_is_cellstr=isstr(' subfieldname(1:j-2) ...
         '(1).' subfieldname(j:end) ');']);
   if sfn_is_cellstr
      tempstr=strrep(tempstr,'[','{');
      tempstr=strrep(tempstr,']','}');
   end;
   eval(tempstr);
end

%_______________________________________________

function s=subfieldnames(a,prefix)
% SUBFIELDNAMES returns ALL fieldnames, not
% just the first tier
%
% written by m. schweisguth, adapted from
% initial version by m. robbins.

if isstruct(a)
else
s = {};
return;
end

if exist('prefix','var')
else
prefix = inputname(1);
end;

ss = struct('type','.','subs','');
fn = fieldnames(a);
cells = cell(1,length(fn));

stringcount = 0;

%note: MATLAB will return the first field value for
% non-homogeneous structures.

for ii=1:length(fn)
ss.subs = fn{ii};
aa = subsref(a,ss);
if isstruct(aa)
cells{ii} = subfieldnames(aa,[prefix '.' fn{ii}]);
stringcount = stringcount + length(cells{ii});
else
cells{ii} = { [prefix '.' fn{ii}] };
stringcount = stringcount + 1;
end;
end;

%collect the results together:

s = cell(1,stringcount);
indx = 1;

for ii=1:length(cells)
fn = cells{ii};
fnii = length(fn);
s(indx:indx+fnii-1) = fn(1:fnii);
indx = indx + fnii;
end;

Michael Robbins, CFA
Director, Proprietary Trading
Debt Capital Markets
Canadian Imperial Bank of Commerce, World Markets
* Sent from RemarQ http://www.remarq.com The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!

2. Where to find the Windows CE Toolkit for Visual Basic 6.0?

3. plot( eval(Qty1), eval(Qty2) ); - bad or worse?

4. Install MSDOS on Novell LAN

5. Convert cell struct to struct array

6. simple question about FFT

7. Eval without eval 2

8. FP2000 & Powerpoint

9. Eval without eval

10. better alternative to eval function....???

11. How to replace eval-function in Matlab?

12. Passing functions to eval.....HELP

13. Can "eval" function be muted?