12.07.2015 Views

Basics of MATLAB and Beyond

Basics of MATLAB and Beyond

Basics of MATLAB and Beyond

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Examples:sprintf(’%0.5g’,(1+sqrt(5))/2) 1.618sprintf(’%0.5g’,1/eps)4.5036e+15sprintf(’%10.3f’,-pi) -3.142sprintf(’%10.3f’,-pi*1000000) -3141592.654sprintf(’%10.3f’,-pi/1000000) -0.000sprintf(’%d’,round(pi)) 3sprintf(’%s’,’hello’)hellosprintf(’The array is %dx%d.’,2,3) The array is 2x3.sprintf(’\n’)Line termination characteron all platformsThese functions are “vectorised”, meaning that if you input a nonscalar,then all the elements will be converted:>> str = num2str(r<strong>and</strong>(3,3),6)str =0.5028130.304617 0.6822230.709471 0.189654 0.3027640.428892 0.193431 0.541674>> size(str)ans =332Exercise 9 Explore the operation <strong>of</strong> the following m-file thatbreaks a sentence up into a list <strong>of</strong> words.function all_words = words(input_string)remainder = input_string;all_words = ’’;while any(remainder)[chopped,remainder] = strtok(remainder);all_words = strvcat(all_words,chopped);endWhy do you think strvcat is used instead <strong>of</strong> char? (Answer onpage 188.)26.5 Using Strings as Comm<strong>and</strong>sThe eval FunctionThe eval function takes a string input <strong>and</strong> executes it as a matlabcomm<strong>and</strong>. For example:>> str = ’v = 1:5’str =v = 1:5c○ 2000 by CRC Press LLC

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

Saved successfully!

Ooh no, something went wrong!