02.03.2016 Views

MATLAB by rudra pratap

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

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

4.3 Language-specific Features<br />

117<br />

else<br />

plot (x , ... )<br />

end<br />

% show the phase plot<br />

4.3.5 Interactive input<br />

The commands input , keyboard, menu, and pause can be used inside a script or<br />

function file for interactive user input. Their descriptions follow.<br />

:rc!r_ on:lifie hotp ·<br />

fyp e:<br />

help }ang<br />

Input<br />

The command input ( 'string'), used in the previous example, displays the text<br />

in string on the screen and waits for the user to give keyboard input.<br />

Examples:<br />

• n=input ( 'Largest square matrix size ') ; prompts the user to input the<br />

size of the "largest square matrix" and saves the input in n.<br />

• more= input ('More simulations? (Y /N) ' , 's') ; prompts the user to type<br />

Y for YES and N for NO and stores the input as a string in more. Note that<br />

the second argument, 's', of the command directs <strong>MATLAB</strong> to save the user<br />

input as a string.<br />

This command can be used to write ·user-jTiendly interactive programs in <strong>MATLAB</strong>.<br />

Keyboard<br />

The command keyboard inside a script or a function file returns control to the<br />

keyboard at the point where the command occurs. The execution of the function<br />

or the script is not terminated. The command window prompt » changes to<br />

k» to show the special status. At this point, you can check variables already<br />

computed, change their values, and issue any valid <strong>MATLAB</strong> commands. The<br />

control is returned to the function <strong>by</strong> typing the word return on the special prompt<br />

k» and then pressing the return/enter key.<br />

This command is useful for debugging functions. Sometimes, in long computations,<br />

you may like to check some intermediate results, plot them and see if the<br />

computation is headed in the right direction, and then let the execution continue.<br />

Example:<br />

% EXKEYBRD : a script file for example of keyboard command<br />

A ones (10) % make a 10x10 matrix of 1s<br />

for i=1 :10<br />

disp(i)<br />

% display the value of i<br />

A(:, i) ; i*A ( : 'i) :<br />

end<br />

% replace the ith column of a<br />

if i==5 % when i = 5<br />

keyboard % return the control to keyboard<br />

end

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

Saved successfully!

Ooh no, something went wrong!