110 likes | 268 Views
Introduction to Matlab:. Matlab Basics. Data Formatting and Saving. S. Awad, Ph.D. M. Corless, M.S.E.E. E.C.E. Department University of Michigan-Dearborn. Data Formatting Topics. Scalar Operations Special Variables and Constants Numerical Formats Format Conversion
E N D
Introduction to Matlab: MatlabBasics Data Formatting and Saving S. Awad, Ph.D. M. Corless, M.S.E.E. E.C.E. Department University of Michigan-Dearborn
Data Formatting Topics • Scalar Operations • Special Variables and Constants • Numerical Formats • Format Conversion • Saving a Session: Diary • Saving Data • Retrieving Data • Saving ASCII Data
Division = a/b = a\b • Exponential = a^b Matlab Operations for Scalars • Addition a+b • Subtraction a-b • Multiplication a*b
i,j; % Special Variables and Constants ans; % Default variable name used for results pi; % Pi ºp = 3.141592… eps; % Smallest computer number = 2.206x10-16 = 2-52 inf; % Stands for ¥ (infinity) nargin; % Number of function input arguments nargout; % Number of function output arguments nan; % 0/0 or ¥ / ¥
Displaying Numbers in Different Formats • Consider the real number • Fixed point format short = 0.3333 ( 5 digits) format long = 0.33333333333333 (15 digits) • Floating point (e) format short e = 3.3333e-001 ( 5 digits) format long e = 3.333333333333333e-001 (15 digits) • Best of fixed or floating point (g) format short g = 0.33333 ( 5 digits) format long g = 0.333333333333333 (15 digits) • Hexadecimal format hex = 3fd5555555555555 (15 digits)
Number Format Conversions • dec2hex converts decimal integers to hexadecimal dec2hex(10) = A • hex2dec converts hexadecimal into decimal integers hex2dec('A') = 10 • dec2base converts a decimal integer to a base Bstring dec2base(23,3) = '212'
Saving a Session: Diary • To copy all subsequent terminal input and most of the resulting output to be stored (as text) in the named file use: diary file_name • To suspend the diary command use: diary off • Note: No graphics are stored
Saving Data • Use the save command to store all variables in Matlab binary format in the file matlab.mat by typing: » save • To save the variables in another file data.mat type: » save data • To save only some variables type: » save a b c; % Save in matlab.mat » save data a b c; % Saves in data.mat • Note a, b, and c are user defined variables • To append data to a file use: » save data d -append % Appends data.mat
Retrieving Saved Data • Use the command loadwith the same syntax as the save command • To retrieve all data stored in matlab.mat » load; • To retrieve all data stored in data.mat » load data;
Saving Data in ASCII • To save variables in 8-digit ASCII format to a file data.txt that can be edited using a text editor: » save data a b c -ascii • To save in 16-digit ASCII format use: » save data a b c -ascii -double • To retrieve the new variable data use the load command with the same syntax: » load data -ascii Data will be a column vector containing elements a b c