1 / 23

M-File, if and for

M-File, if and for. Arief Ramadhan. Create M-File. Select File -> New -> M-file from the MATLAB Desktop The Editor/Debugger will opens, if it is not already open, with an untitled file in the MATLAB current directory from which you can create an M-file. Functions.

margo
Download Presentation

M-File, if and for

An Image/Link below is provided (as is) to download presentation 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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. M-File, if and for AriefRamadhan

  2. Create M-File • Select File -> New -> M-file from the MATLAB Desktop • The Editor/Debugger will opens, if it is not already open, with an untitled file in the MATLAB current directory from which you can create an M-file.

  3. Functions • Functions are M-files that can accept input arguments and return output arguments. • The name of the M-file and of the function should be the same. • Functions operate on variables within their own workspace, separate from the workspace you access at the MATLAB command prompt.

  4. The first line • The function definition line • The first line of a function M-file starts with the keyword function. • It gives the function name and order of arguments. input arguments • If no output argument is supplied, the result is stored in ans

  5. The body • The rest of the file is the executable MATLAB code defining the function. • All variables introduced in the body of the function, are all local to the function; they are separate from any variables in the MATLAB workspace.

  6. Example function mysquare(x) y = x*x;  % this is comment % disp is used to show something disp (‘The result is:'); disp (y);

  7. To save • File  Save • File  Save As • Where you assign a name to the file and save it. You do not need to type the .m extension because MATLAB automatically assigns the .m extension to the filename. • Please save in your own direcory

  8. Example

  9. Running in Command Window • First, choose/browse your own directory in the Current Directory field

  10. Running in Command Window (2) • Then, type your function name (mysquare) with the output/input arguments

  11. Result

  12. Function with a return value • Using output argument • Example : function x=mytotal(a) % x is output argument x = a+5;

  13. How to run • Using variable to save/hold the return value • Example

  14. Function with multiple return value • Using multiple output argument • Separate by comma (,) • Using ‘[‘ and ‘]’ • Example : function [x,y]=mytotal2(a) x = a+5; y = a*5;

  15. How to run

  16. if • Conditionally execute statements if expression statements end • Example if x<2 z=7; end

  17. For • Repeat statements a specific number of times • The columns of the expression are stored one at a time in the variable while the following statements, up to the end, are executed. • In practice, the expressionis almost always of the form scalar : scalar, • in which case its columns are simply scalars. • The scope of the for statement is always terminated with a matching end.

  18. Example for i=1:10 disp(i); end

  19. Exercise • Build a function/M-file to solve Ax2 + Bx + C

More Related