3) Scripts and functions (also known by their extension as .m files) are ways of
ID: 3562190 • Letter: 3
Question
3) Scripts and functions (also known by their extension as .m files) are ways of organizing code in units or modules. Use the links below to answer the following questions: MATLAB scripts and functions:
http://www.mathworks.com/help/matlab/learn_matlab/scripts-and-functions.html MATLAB creating and running scripts
http://www.mathworks.com/help/matlab/matlab_prog/create-scripts.html
a) What is the difference between a script and function?
b) How do you create a new script or function in MATLAB?
c) List two ways to run a script in MATLAB
Explanation / Answer
1. script is a file than can be run directly .
but function has certian inputs andoutputs and you need to provide the required number of inputs to invke the function.
example:
let me describe a function
-------------------------------
function [y]=my_sine(x)
y=sin(x);
end
------------------------------
i can not directly call my_sine...i have to provide the argument. for example,the correct way of invoking the function is y=my_sine(pi/6)...this will print y=0.5.
now,for a script,if you write,
a=3;
b=4;
disp(a)
disp(b)
and store it ,lets say, in myscript.m. then in the workspace, you can directly call the script by typing
myscript
it will print
3
4
in the workspace.
Q2.to create a new script, there are 3 ways.
a)Highlight commands from the Command History, right-click, and select Create Script.
b)Click the New Script button on the Home tab.
c)Use the edit function
e.g. edit file_name
Q3.to run a script:
lets the name of the script is myscript.m
a)Type the script name myscript on the command line and press Enter
b)Click the Run button on the Editor tab
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.