0% found this document useful (0 votes)
16 views13 pages

L5 Scripts

Uploaded by

ibxbi0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views13 pages

L5 Scripts

Uploaded by

ibxbi0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Scripts Functions and

workspace
Scripts
• Scripts are m-files containing MATLAB statements.
• Script files can only operate on the variables that are hard-coded into
their m-file.
• Scripts are useful for tasks that don't change.
• No input and output arguments
• They are also a way to document a specific sequence of actions, say a
function call with special parameter values, that may be hard to
remember.
Scripts
• A script can be thought of as a keyboard macro: when you type the
name of the script, all of the commands contained in it are executed
just as if you had typed these commands into the command window.
• Thus, all variables created in the script are added to the workspace
for the current session.
• Furthermore, if any of the variables in the script file have the same
name as the ones in your current workspace, the values of those
variables in the workspace are changed by the actions in the script.
• This can be used to your advantage. It can also cause unwanted side
effects.
Functions
• Functions much more flexible.
• They are more suitable for general purpose tasks.
• MATLAB ``functions'' are another type of m-file.
• Functions have input and output parameters.
• function variables are local to the function.
• it's possible to declare and use global variables, but that requires and
explicit action by the user.)
• The local scope of function variables gives you greater security and
flexibility.
• The only way (besides explicitly declared global variables) to get
information into and out of a function is through the variables in the
parameter lists.
Creating a script m-file

1. Click the New Script button on the Home tab.

2. Highlight commands from the Command History, right-click, and


select Create Script. (not working in latest versions)

3. Use the edit function.


1. For example
2. edit new_file_name
3. creates (if the file does not exist) and opens the file new_file_name.
Creating a script m-file
Saving scripts .m files
• First create a folder for your
matlab files in any drive
• Make the folder your ‘current
folder’
• Save the script with .m extension
Running a script
• Type the name of the script (without extension) in the command
widow
• For example
• >>areat
• You will get your results and variables in the workspace window
Save and Load Workspace Variables

• The workspace is not maintained across sessions of MATLAB. When you


quit MATLAB, the workspace clears.
• However, you can save any or all the variables in the current workspace
to a MAT-file (.mat).
• You can then reuse the workspace variables later during the current
MATLAB session or during another session by loading the saved MAT-
file.
Save Workspace Variables

• There are several ways to save workspace variables interactively:


• To save all workspace variables to a MAT-file, on the Home tab, in
the Variable section, click Save Workspace.
• To save a subset of your workspace variables to a MAT-file,
• select the variables in the Workspace browser, right-click, and then
select Save As.
• You also can drag the selected variables from the Workspace browser to the
Current Folder browser.
Save Workspace Variables

You also can save workspace variables programmatically


using the save function. For example, to save all current
workspace variables to the file wp1.mat, use the command
save(‘wp1')
To save only variables A and B to the file wp1.mat, use the
command
save(‘wp1','A','B')
Load Workspace Variables

You can load saved variables in command window, by using the load
function.
For example, load all variables from the file wp1.mat
load(‘wp1')
To load variables from the file wp1.mat
load(‘wp1',‘D',‘E')
Load Workspace Variables

• To load saved variables from a MAT-file into your workspace, double-


click the MAT-file in the Current Folder browser.
• To load a subset of variables from a MAT-file on the
• Home tab,
• in the Variable section,
• click Import Data.
• Select the MAT-file you want to load and click Open. You also can drag the
desired variables from the Current Folder browser Details panel of the
selected MAT-file to the Workspace browser.

You might also like