Page 14 - Applied Numerical Methods Using MATLAB
P. 14
BASIC OPERATIONS OF MATLAB 3
>>A %what is the value of A?
??? Undefined function or variable ’A’
>>load ABC A C %read the values of A,C from the file ’ABC.mat’
>>A %the value of A
A = 1 2 3
4 5 6
If you want to store the data into an ASCII dat-file (in the current directory),
make the filename the same as the name of the data and type ‘/ascii’atthe
end of the save statement.
>>save B.dat B /ascii
However, with the save/load commands into/from a dat-file, the value of only
one variable having the lowercase name can be saved/loaded, a scalar or a vec-
tor/matrix. Besides, non-numeric data cannot be handled by using a dat-file. If
you save a string data into a dat-file, its ASCII code will be saved. If a dat-file
is constructed to have a data matrix in other environments than MATLAB, every
line (row) of the file must have the same number of columns. If you want to read
the data from the dat-file in MATLAB, just type the (lowercase) filename ***.dat
after ‘load’, which will also be recognized as the name of the data contained in
the dat-file.
>>load b.dat %read the value of variable b from the ascii file ’b.dat’
On the MATLAB command line, you can type ‘nm112’ to run the following
M-file ‘nm112.m’ consisting of several file input(save)/output(load) statements.
Then you will see the effects of the individual statements from the running
results appearing on the screen.
%nm112.m
clear
A=[123;456]
B = [3;-2;1];
C(2)=2;C(4)=4
disp(’Press any key to see the input/output through Files’)
save ABCABC %save A,B & C as a MAT-file named ’ABC.mat’
clear(’A’,’C’) %remove the memory about A and C
load ABC A C %read MAT-file to recollect the memory about A and C
save B.dat B /ascii %save B as an ASCII-file named ’b.dat’
clear B
load b.dat %read ASCII-file to recollect the memory about b
b
x = input(’Enter x:’)
format short e
x
format rat, x
format long, x
format short, x