1 / 10

Matlab Basics

Learn how to manipulate scalar operations, special variables, numerical formats, and efficient data saving techniques in Matlab. Discover ways to format, save, and retrieve your data with ease. Save in ASCII format for easy editing.

jbox
Download Presentation

Matlab Basics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. Data Formatting Topics • Scalar Operations • Special Variables and Constants • Numerical Formats • Format Conversion • Saving a Session: Diary • Saving Data • Retrieving Data • Saving ASCII Data

  3. Division = a/b = a\b • Exponential = a^b Matlab Operations for Scalars • Addition a+b • Subtraction a-b • Multiplication a*b

  4. 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 ¥ / ¥

  5. 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)

  6. 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'

  7. 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

  8. 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

  9. 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;

  10. 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

More Related