MATLAB Second Seminar. Previous lesson. Last lesson We learnt how to: Interact with MATLAB in the MATLAB command window by typing commands at the command prompt. Define and use variables. Plot graphs.
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
MATLABSecond Seminar
Last lesson We learnt how to:
It would be nice if you didn't have to manually type these commands at the command prompt whenever you want to use them.
while 1 % use of an infinite loop
TinF = input('Temperature in F:'); % get input
if isempty(TinF) % how to get out
break
end
TinC = 5*(TinF - 32)/9; % conversion
disp(' ')
disp([' ==> Temperature in C =',num2str(TinC)])
disp(' ')
end
Can’t be displayed on screen
Ca be displayed on screen
sin % Sine.
exp % Exponential.
abs % Absolute value. round % Round towards nearest integer.
lcm % Least common multiple.
cart2sph % Transform Cartesian to spherical%coordinates.
Keyword: function
Function Name (same as file name .m)
Output Argument(s)
Input Argument(s)
Online Help
MATLAB
Code
Command Line Syntax
function y = mean(x)
% MEAN Average or mean value.
% For vectors, MEAN(x) returns the mean value.
% For matrices, MEAN(x) is a row vector
% containing the mean value of each column.
[m,n] = size(x);
if m == 1
m = n;
end
y = sum(x)/m;
Multiple Input Arguments ( , )
functionr = ourrank(X,tol)
% OURRANK Rank of a matrix
s = svd(X);
if(nargin == 1)
tol = max(size(X))*s(1)*eps;
end
r = sum(s > tol);
Multiple Output
Arguments [ , ]
function[mean,stdev] = ourstat(x)
% OURSTAT Mean & std. deviation
[m,n] = size(x);
ifm == 1
m = n;
end
mean = sum(x)/m;
stdev = sqrt(sum(x.^2)/m – mean.^2);
- Many of Matlab functions can be run with different number of input variables.
- efficiency
Global variables can be shared by multiple workspaces.
(Must be initialized in all relevant workspaces.)
>>whos global%shows the contents of the global workspace
>>clear global %erases the variable from both local and global workspaces.
>>isglobal()
High
variable
built-in function
subfunction
private function
MEX-file
P-file
M-file
Low
» cos='This string.';
» cos(8)
ans =
r
» clear cos
» cos(8)
ans =
-0.1455
Select Workspace
Set Auto-
Breakpoints
Set Breakpoint
Clear Breaks
Step In
Single Step
Continue
Quit Debugging
Current
Location
Current Workspace (Function)
Error message
Access to Function’s Workspace
Debug Mode
test.m
functionTinC=temp2(TinF)
TinF = input('Temperature in F:'); % get input
if nargin==0 % if there is no input
disp('no temparture was entered');
TinC=nan;
else
TinC = 5*(TinF - 32)/9; % conversion
disp(' ')
disp([' ==> Temperature in C =',num2str(TinC)])
disp(' ')
end
To read files in
>> guide
OR:
Push buttons
axes
Static text
Pop-up menu
Component panel
Drag to resize
Layout area
Activate GUI
Property
Inspector