Lecture 25
Lecture 25
Where do we go from
here?
So far we have used Matlab in two ways:
As a scratch pad in the command window
To write simple programs (scripts) in the editing
window
In both of these, we assumed that the programmer
was the user
Now we will move on to more complicated
techniques for use when the user and programmer
are different people
Allow input/output techniques
Logic for decision making
5.2.1 User-Defined Input
We can create more general programs by
allowing the user to input values
z = input(‘Enter a value’)
% Displays Enter a value to the screen
% If the user enters 5 at the prompt, then z = 5
z = 1 2 3
4 5 6
5.2.2 Output Options
The disp() function has the format of
disp(‘message’)
disp(variable)
temp = 98.6;
fprintf(‘The temperature is %f degrees F \n’, temp)
temp = 98.6;
fprintf(‘The temperature is %8.2f degrees F \n’, temp)
B = [8.8 7.7 ;
8800 7700];
fprintf('X is %4.2f meters or %8.3f mm\n', 9.9, 9900, B)
MATLAB displays:
X is 9.90 meters or 9900.000 mm
X is 8.80 meters or 8800.000 mm
X is 7.70 meters or 7700.000 mm
Questions???
5.3 Functions in Matlab
The following would be in its own separate M-file: