80 likes | 99 Views
File Operations in Matlab. Load / Save *.dat, *.mat vs. -ascii fopen /fclose. Get data from other applications Save variables for later use. >> x = 1:10 x = 1 2 3 4 5 6 7 8 9 10 >> y = x .^ 2 y =
E N D
File Operations in Matlab Load / Save *.dat, *.mat vs. -ascii fopen /fclose
Get data from other applications Save variables for later use >> x = 1:10 x = 1 2 3 4 5 6 7 8 9 10 >> y = x .^ 2 y = 1 4 9 16 25 36 49 64 81 100 >> save x.mat x y >> load x.mat Load and Save
Save variable data into file with standard character format Open or import into spreadsheets, database, word processor >> x x = 1 2 3 4 5 6 7 8 9 10 >> y y = 1 4 9 16 25 36 49 64 81 100 >> save x.txt x y -ascii >> -ascii option
Open files and write or read data into them. Use the file ID # to specfiy placement of data. fprintf writes formatted data to files >> fid = fopen('x.mat', 'w+'); >> load x.txt >> fprintf(3, ' %6.4f\n ', x); >> fid fid = 3 >> fclose(fid); >> fopen / fclose
results x.txt x.mat
Hints 5.18: Use example 4.7 (p.166) • Turn this into a function: function [slope, y_int] = lsqfit(x,y) • Functions: section 5.1 (p. 190)
Hints • 5.22 • Use rand function to generate random number. • Scale from [0,1) interval to [-1,1) • Make user-defined function to transform uniform to normal • use this function in m-file to generate array • use hist() function, and std() built in functions
Hints • 7.6 [structure arrays] • Define structure array in m-file • Create user defined function that • accepts structure array • plots the info in the array • section 7.3.4-5 (p. 310 -311)