Lecture 4: Matrix Transformations, MatLab Scripts Functions, Serial Communication Keyword “function” tells us this is a function file (called “perim m”)
Previous PDF | Next PDF |
[PDF] Functions and Scripts - Eecs Umich
can then be used and reused just like intrinsic MATLAB functions A function file is a file with an Rounding and Remainder Function Description ceil(x) round x
[PDF] MATLAB Tutorial Chapter 6 Writing and calling functions
In MATLAB, one writes first a master file for the program, either a script file or better yet a function m-file that returns a single integer (that might return 1 for program
[PDF] MATLAB Tutorial Chapter 6 Writing and calling functions
MATLAB allows much latitude in writing functions with variable length argument and output variable lists For example, the function could also be invoked by the command : outvar1 = filename(var1,var2, )
[PDF] MATLAB Examples - Scripts and User-defined Functions
subplot(2,2,4) plot(x,y4) MATLAB Scripts are saved as so-called m files (file extension is m) Test the function afterwards in the Command window as follows:
[PDF] Part I: MATLAB Overview - SPIE
Move the mouse cursor to New File and then to Script and approach of having the initial program be a MATLAB script MATLAB function for the initial file
[PDF] MATLAB Programming Tips
You can enter MATLAB commands using either a command or function syntax MATLAB scripts store their variables in a workspace that is shared with the
[PDF] EGR111 MATLAB Script Files vs Functions
Never contains the “clear” command Uses the workspace variables, which remain after the script is finished Variables inside the function are private to the
[PDF] Lecture 4: Matrix Transformations, MatLab Scripts & Functions
Lecture 4: Matrix Transformations, MatLab Scripts Functions, Serial Communication Keyword “function” tells us this is a function file (called “perim m”)
[PDF] TP 3 : Les fonctions dans Matlab 1 Ecrire des fonctions en Matlab
function [res] = produit(a,b) res = a*b; end Pour tester cette fonction, placez-vous (`a l'aide de l'explorateur de fichiers Matlab) dans le répertoire qui contient le
[PDF] matlab graphics
[PDF] matlab high quality figures
[PDF] matlab object oriented programming
[PDF] matlab object oriented programming pdf
[PDF] matlab plot color coded
[PDF] matlab plot colormap
[PDF] matlab program to calculate fourier series coefficients associated with square wave
[PDF] matlab programming questions and answers pdf
[PDF] matlab programs examples pdf
[PDF] matlab return value from function
[PDF] matlab solve quadratic equation
[PDF] matlab solve simultaneous equations
[PDF] matlab solve system of nonlinear equations multiple solutions
[PDF] matrice carrée d'ordre 1
ProfessorCarrEverbachCoursewebpage:
E5... Comments on Response Readings Thursday 9/22: Matlab I is due. Thursday 9/29: MatLab II lab is due. Wizards available Tuesdays/Wednesdays 7:00-9:00 pm (Hicks 213) for Engin (Matlab help) and Sundays/Tuesdays 7:00-9:00 pm (SC 128) Physics & Math This week in lab: make things out of stuff. If time, start assembling servos into robotic arm.From Elektronik (a Swedish Engineering Magazine)
ControlofflowinMatLab(Ch4)
So far we have used MatLab as a fancy calculator The real power comes when we can have MatLab: Perform operations many times consecutively:
for... loop Make decisions: if...then...else... if...then...elseif...then...elseif... ... ...else... while... loop
Thefor...loop(1)
Example:
for i=1:10 disp(i) end Output: 1 2 3 4 5 6 7 8 9 10 Syntax: for variable=start:finish ...commands... endThefor...loop(2)
Example:
for i=1:4:10 disp(i) endOutput: 1 5 9 Syntax:
for variable=start:increment:finish ...commands... endMakingDecisionsinMatlab:if
Syntax:
if expression, ... statements ... endExample:
if a>b, disp('a is greater than b'); end if (a>b) & (a>0), disp('a>b and a is positive'); end expression has to evaluate to TRUE or FALSELogicalexpressions
a>b TRUE when a is greater than b, otherwise FALSE expression has to evaluate to TRUE or FALSEa=b TRUE when a is greater than or equal to b, otherwise FALSE a==b TRUE when a is equal to b, otherwise FALSE a~=b TRUE when a is not equal to b, otherwise FALSE (a>0) & (b>0) TRUE when a is greater than 0 and b is greater than zero, otherwise FALSE (a>0) | (b>0) TRUE when a is greater than 0 or b is greater than zero, otherwise FALSE
(a>0) | ~(b>0)TRUE when a is greater than 0 or b is not greater than zero, otherwise FALSEBe careful if a or b are matrices, surprising results can occur! Note: this list is not exhaustive, check you text for more.
MakingDecisions:if ... else
Syntax:
if expression, ... statements ... else ... statements endExample:
if a>b disp('a is greater than b'); else disp('a is less than or equal to b'); endMakingDecisions:if ... elseif ... else
Syntax:
if expression1 ... statements ... elseif expression2 ... statements ... else ... statements ... end
Example:
if a>b disp('a is greater than b'); elseif a MakingDecisions:while...
Syntax:
while expression ... statements ... end Example:
i=1; while i<10 disp(i); i=i+4; end Output: 1 5 9
Note: this is equivalent to the for...loop used earlier FuncEons
For our purposes a function is a special MatLab file that does a calculation(s) and returns a result
It is different from a script file: it returns a result it can't change other variables that you are usingAnatomyofaFuncEon
function p = perim(xvals,yvals) x1=[xvals xvals(1)];%Form augmented vector y1=[yvals yvals(1)]; xd=diff(x1); %Calculate differences yd=diff(y1); sqrd = xd.^2+yd.^2; %Square them sqrtSqd=sqrt(sqrd); %Find distances p=sum(sqrtSqd); %Find perimeter
Keyword "function" tells us this is a function file (called "perim.m") Output variable(s) name(s); in this case only one: the variable "p" Function name; also name of ".m" file Input variables Calculations Output variable (must be the same variable name as is in function declaration)
UsingFuncEons
Now we can call function as if it was built in:
>> q=perim(x,y) q = 6.8284 A trick: if you add comments to the very top of the function file, they will appear if you ask for documentation (i.e., ">>doc perim").
Caveats
Function name must not be the same as a variable name Function name must have no spaces in it Function must be in MatLab directory so MatLab can find it. If you edit a function, you must save the file before the changes will take effect in subsequent calls If you edit a function, you must save the file before the changes will take effect in subsequent callsSerialCommunicaEoninMatlab
Matlabcommunicatesthroughtheserialportviafprintfandfscanfcommandswritingtoa"serialportobject."
s=serial('COM1');%createsaserialportobject set(s,'Baudrate',38400);%setsbaudrateofCOM1 fprintf(s,'#0P1500');%writestextinsinglequotesto