12.07.2015 Views

Basics of MATLAB and Beyond

Basics of MATLAB and Beyond

Basics of MATLAB and Beyond

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

eval(str)v =1 2 3 4 5The eval(str) statement acts just as if we had typed v = 1:5 at thecomm<strong>and</strong> line. To suppress the output we need to add a semicoloncharacter to the end <strong>of</strong> the string:>> str = ’v = 1:5;’str =v = 1:5;>> eval(str)The eval comm<strong>and</strong> now produces no output, while still defining thevariable v. To take another example, let us suppose we want to define aset <strong>of</strong> vectors v i =1, 2,...i for i =1, 2 ...10. At the comm<strong>and</strong> line wecould type:v1 = 1;v2 = 1:2;v3= 1:3;<strong>and</strong> so on. The eval comm<strong>and</strong> provides a neat solution:>> clear>> for i = 1:10str = [’v’ int2str(i) ’ = 1:i;’];eval(str)endThis has generated the variables v1, ..., v10, with the appropriate elements:>> whosName Size Bytes Classi 1x1 8 double arraystr 1x10 20 char arrayv1 1x1 8 double arrayv10 1x10 80 double arrayv2 1x2 16 double arrayv31x3 24 double arrayv4 1x4 32 double arrayv5 1x5 40 double arrayv6 1x6 48 double arrayv7 1x7 56 double arrayv8 1x8 64 double arrayv9 1x9 72 double arrayGr<strong>and</strong> total is 66 elements using 468 bytesc○ 2000 by CRC Press LLC

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!