Introduction to Matlab Matlab Basics Data Formatting and

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

» MATLAB Basics: Data Formatting Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Data Formatting Topics n n n n Scalar Operations Special Variables and Constants Numerical Formats Format Conversion Saving a Session: Diary Saving Data Retrieving Data Saving ASCII Data 2

» MATLAB Basics: Data Formatting Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Matlab Operations for Scalars Addition a+b n Subtraction Multiplication a-b a*b n Division n n = a/b = ab n Exponential = a^b 3

» MATLAB Basics: Data Formatting Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Special Variables and Constants ans; % Default variable name used for results pi; % Pi º p = 3. 141592… eps; % Smallest computer number = 2. 206 x 10 -16 = 2 -52 inf; % Stands for ¥ (infinity) i, j; % nargin; % Number of function input arguments nargout; % Number of function output arguments nan; % 0/0 or ¥ / ¥ 4

MATLAB Basics: Data Formatting » Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Displaying Numbers in Different Formats n n n Consider the real number Fixed point format short = 0. 3333 ( 5 digits) format long = 0. 3333333 (15 digits) Floating point (e) format short e = 3. 3333 e-001 ( 5 digits) format long e = 3. 33333333 e-001 (15 digits) Best of fixed or floating point (g) format short g = 0. 33333 ( 5 digits) format long g = 0. 33333333 (15 digits) Hexadecimal format hex = 3 fd 5555555 (15 digits) 5

» MATLAB Basics: Data Formatting Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Number Format Conversions n dec 2 hex converts decimal integers to hexadecimal dec 2 hex(10) = A n hex 2 dec converts hexadecimal into decimal integers hex 2 dec('A') = 10 n dec 2 base converts a decimal integer to a base B string dec 2 base(23, 3) = '212' 6

MATLAB Basics: Data Formatting » Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Saving a Session: Diary n 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 n To suspend the diary command use: diary off n Note: No graphics are stored 7

» MATLAB Basics: Data Formatting Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Saving Data n Use the save command to store all variables in Matlab binary format in the file matlab. mat by typing: » save n To save the variables in another file data. mat type: » save data n To save only some variables type: » save a b c; » save data a b c; u n % Save in matlab. mat % 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 8

» MATLAB Basics: Data Formatting Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Retrieving Saved Data n n Use the command load with the same syntax as the save command To retrieve all data stored in matlab. mat » load; n To retrieve all data stored in data. mat » load data; 9

» MATLAB Basics: Data Formatting Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Saving Data in ASCII n n n 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 10
- Slides: 10