You are on page 1of 23

3.

7 Saving and Loading Data


The save command can be used to save either the entire workspace or a few selected
variables in a file called Mat-file.
The data saved in these files can be loaded into the MATLAB workspace by the load
command.
Examples-
save tubedata.mat x y - saves variables x and y in the file tubedata.mat
save new data rx ry rz - saves variables rx, ry, and rz in the file newdata.mat
save xdata.dat x-ascii - saves variable x in the file xdata.dat in 8-digit ASCII format
save - saves the entire workspace in the file matlab.mat
load tubedata - loads the variables saved in the default file matlab.mat
Function Files
Function files are like programs or subroutines in Fortran, procedures in Pascal,
and functions in C.
A function file begins with a function definition line, which has a well-defined list
of inputs and outputs.
Syntax - function [ output variables] = function_name (input variables) ;

where the function_name must be the same as the file name.


Examples: Function Definition Line File Name
function [rho,H,F] = motion( x,y,t ); motion.m
function [theta] = angleTH ( x,y); angleTH.m
function [ ] = circle (r ); circle.m
Anatomy of a function file
4.2.4 Subfunctions
MATLAB allows several functions to be written in a single file.
The first function can access all its subfunctions.
You can get on-line help on a subfunction by typing
help m_filename> subfunction_name
4.2.5 Nested functions:
Nested functions are functions written inside a main function, just like
subfunctions.
Each nested function must be terminated by an end statement.
Example
Example:
function [ x, y ] = main_fun (t, a, b)
:
function x = nested_fun1 (a, b)
:
end
function y = nested_fun2(t)
:
end
end
Here, nested_fun1 and nested_fun2 are nested functions inside the
main function main_fun.
4.3.2 Continuation
An ellipsis (three consecutive periods) at the end of a line denotes
continuation.
if you type a command that does not fit on a single line, you may split
it across two or more lines by using an ellipsis
Example:
4.3.3 Global variables
It is possible to declare a set of variables to be globally accessible to all or some
functions without passing the variables in the input list.
This is done with the global command.
Example: Consider solving the following first-order ODE:
x = kx + c sin t, x ( O ) = 1.0
This, however, wont work. In order for ode1 to compute xdot, the values of k and c
must be prescribed.
An alternative is to prescribe the values in the script file and make them available to
the function ode1 through global declaration.
4.3.4 Loops , branches, and control-flow
For loops
A for loop is used to repeat a statement or a group of statements for a fixed
number of times.
Example 1: for m=1 : 100
num = 1 / (m+ 1 )
end
Example 2 : for n= 100 : -2 : 0 , k = 1 / ( exp (n) ) , end
The counter in the loop can also be given explicit increment:
for i =m: k: n to advance the counter i by k each time.
While loops
A while loop is used to execute a statement or a group of statements for
an indefinite number of times until the condition specified by while is no
longer satisfied.
Example: % let us find all powers of 2 below 10000
v = 1 ; num = 1 ; i = 1 ;
while num < 10000
v = [v ; num] ;
i=i+1;
num =2^I ;
end
If-elseif-else statements
This construction provides a logical branching for computations.
Example:
i=6 ; j =21 ;
if i > 5
k= i;
else if (i>1) & (j ==20)
k= 5 * i + j ;
else
k=1 ;
end
Switch-case-otherwise
A flag (any variable) is used as a switch and the values of the flag make up the
different cases for execution.
The general syntax is: switch flag
case value 1
block 1 computation
case value2
block 2 computation
otherwise
last block computation
end
Here, block 1 computation is performed if flag=value1 , block 2 computation is
performed if flag=value2 , and so on.
The switch can be a numeric variable or a string variable.
Example- using a string variable as the switch
c o l o r = input ( ' color = ' , ' s ' ) ;
switch color
case ' red '
c = [ 1 0 0] ;
case ' gr e en '
c = [0 1 0] ;
case ' blue '
c = [0 0 1 ] ;
otherwise
error ( ' invalid choice of color ' )
end
Break
The command break inside a f o r or while loop terminates the execution of
the loop, even if the condition for execution of the loop is true.
Examples:
Error
The command error ( ' message ' ) inside a function or a script aborts the
execution, displays the error message message, and returns the control
to the keyboard.
Example
Return
The command return simply returns the control to the invoking
function.
Example: function animatebar ( t0, tf , x0) ;
% animatebar animates a bar pendulum.
disp ( ' Do you want to see the phase portrait? ' )
ans = input ( ' Enter 1 if YES , 0 if NO ' ) ;
% see text for description
if ans==0 % if the input is 0
return % exit function
else
plot(x,..) % show the phase plot
end
4 . 3 . 5 Interactive input
Input
The command input ( ' string ' ) , displays the text in string on the
screen and waits for the user to give keyboard input .
Examples:
Keyboard
The command keyboard inside a script or a function file returns control
to the keyboard at the point where the command occurs.
This command is useful for debugging functions.
Example:
Menu
The command menu ( MenuName , option1 , option2 , ..) creates
an on-screen menu with the MenuName and lists the options in the
menu.
Example:
Pause
The command pause temporarily halts the current process. It can be
used with or without an optional argument:
Pause halts the current process and waits for the user to give a
"go-ahead signal. Pressing any key resumes the process .
Example: for i=1 : n , plot (X ( : , i) , Y ( : , i ) ) , pause , end.
pause ( n) halts the current process, pauses for n seconds, and then
resumes the process.
Example: for i=1 : n, plot (X ( : , i ) , Y ( : , i ) ) , pause( 5 ) , end
pauses for five seconds before it plots the next graph.
4.3.7 Input/output
MATLAB supports many standard C-language file I/0 functions for
reading and writing formatted binary and text files.
Here is a simple example that uses fopen, fprintf, and fclose to create and
write formatted data to a file:

In this script file, the first I/0 command, fopen, opens a file Temperature.table in the
write mode and assigns the file identifier to fid.
The output file, Temperature.table, is shown here. Note that the data
matrix t has two rows, whereas the output file writes the matrix in two
columns.

You might also like