eval and fprintf

  • Follow


Hi,

I'm sometime using eval and fprintf when I want to create different variable names in a loop. It is usefull when I work with vectors of different length.

Here is the command i'm using :

eval(fprintf('tdate%d = rawdata%d(:,3);',Names(1,i),Names(1,i)))

Names is a 1x2 matrix --> [80, 20]

And to my suprise, here is the error message :

tdate80 = rawdata80(:,3);??? Undefined function or method 'eval' for input arguments of type 'double'.

Can someone help me with that or teach me a more efficient way if there is one?

Thank you very much!

JP
0
Reply Jean 10/7/2010 11:49:03 PM

Get rid of the eval() - it's totally unnecessary.  
Just have the fprintf() by itself.
0
Reply Image 10/8/2010 1:45:05 AM


"Image Analyst" <imageanalyst@mailinator.com> wrote in message <i8lt31$ejs$1@fred.mathworks.com>...
> Get rid of the eval() - it's totally unnecessary.  
> Just have the fprintf() by itself.
0
Reply Matt 10/8/2010 4:09:04 AM

"Matt Fig" <spamanon@yahoo.com> wrote in message <i8m5h0$ol6$1@fred.mathworks.com>...
> "Image Analyst" <imageanalyst@mailinator.com> wrote in message <i8lt31$ejs$1@fred.mathworks.com>...
> > Get rid of the eval() - it's totally unnecessary.  
> > Just have the fprintf() by itself.

Ok thanks!
0
Reply Jean 10/8/2010 12:56:03 PM


"Jean-Philip Dumont" <jean-philip.dumont@hec.ca> wrote in message 
news:i8lm9f$1tq$1@fred.mathworks.com...
> Hi,
>
> I'm sometime using eval and fprintf when I want to create different 
> variable names in a loop. It is usefull when I work with vectors of 
> different length.

See question 4.6 in the newsgroup FAQ for an explanation of why this is a 
Bad Idea and some alternatives you should be using instead.

> Here is the command i'm using :
>
> eval(fprintf('tdate%d = rawdata%d(:,3);',Names(1,i),Names(1,i)))
>
> Names is a 1x2 matrix --> [80, 20]
>
> And to my suprise, here is the error message :
>
> tdate80 = rawdata80(:,3);??? Undefined function or method 'eval' for input 
> arguments of type 'double'.
>
> Can someone help me with that or teach me a more efficient way if there is 
> one?

The reason for the error is because FPRINTF does NOT return a string 
containing the text that it wrote to the file (which could be "standard 
output", the Command Window, if no file ID was specified) but something 
else.

http://www.mathworks.com/help/techdoc/ref/fprintf.html?BB=1

If you MUST do this (instead of using the preferred alternatives in the 
answer to question 4.6, for example if someone says "If you don't create 
this variable, we'll kill this dog" [*]) then you want SPRINTF instead.

-- 
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on 
http://www.mathworks.com

[*] http://en.wikipedia.org/wiki/File:Natlamp73.jpg 

0
Reply Steven_Lord 10/8/2010 3:35:16 PM

4 Replies
255 Views

(page loaded in 0.058 seconds)

Similiar Articles:











7/26/2012 8:19:22 PM


Reply: