fprintf and other examples. save command >> !ls >> a = 3; >> save afile a >>...

12
fprintf and other examples

Upload: dinah-mclaughlin

Post on 17-Dec-2015

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Fprintf and other examples. Save command >> !ls >> a = 3; >> save afile a >> !ls afile.mat >> !dir Directory of C:\MATLAB6p5\work\save 11/21/2003 09:30

fprintf and other examples

Page 2: Fprintf and other examples. Save command >> !ls >> a = 3; >> save afile a >> !ls afile.mat >> !dir Directory of C:\MATLAB6p5\work\save 11/21/2003 09:30

Save command

>> !ls>> a = 3;>> save afile a>> !lsafile.mat >> !dir Directory of C:\MATLAB6p5\work\save 11/21/2003 09:30 AM <DIR> . 11/21/2003 09:30 AM <DIR> .. 11/21/2003 09:30 AM 184 afile.mat 1 File(s) 184 bytes

Page 3: Fprintf and other examples. Save command >> !ls >> a = 3; >> save afile a >> !ls afile.mat >> !dir Directory of C:\MATLAB6p5\work\save 11/21/2003 09:30

Load Command

>> clear a;>> whos>> load afile.mat>> whos Name Size Bytes Class

a 1x1 8 double array

Grand total is 1 element using 8 bytes

Page 4: Fprintf and other examples. Save command >> !ls >> a = 3; >> save afile a >> !ls afile.mat >> !dir Directory of C:\MATLAB6p5\work\save 11/21/2003 09:30

Load/save entire workspace

>> v = 1:5;>> m = ones(2, 3);>> whoYour variables are:a m v >> save allfile>> !lsafile.mat allfile.mat >> clear>> whos>> load allfile.mat>> whoYour variables are:a m v

Page 5: Fprintf and other examples. Save command >> !ls >> a = 3; >> save afile a >> !ls afile.mat >> !dir Directory of C:\MATLAB6p5\work\save 11/21/2003 09:30

Ascii Format>> save -ascii allfile.dat>> !more allfile.dat 3.0000000e+000 1.0000000e+000 1.0000000e+000 1.0000000e+000 1.0000000e+000 1.0000000e+000 1.0000000e+000 1.0000000e+000 2.0000000e+000 3.0000000e+000 4.0000000e+000 5.0000000e+000 >> clear>> load -ascii allfile.dat??? Error using ==> loadNumber of columns on line 2 of ASCII file C:\MATLAB6p5\work\save\allfile.datmust be the same as previous lines.>> who>> m = ones(2, 3);>> save -ascii m.dat>> clear>> load -ascii m.dat>> mm = 1 1 1 1 1 1

Page 6: Fprintf and other examples. Save command >> !ls >> a = 3; >> save afile a >> !ls afile.mat >> !dir Directory of C:\MATLAB6p5\work\save 11/21/2003 09:30

File Example

filename = input('Enter file name: ','s');out_array = randn(1,10000);% Open the output file for writing.[fid,msg] = fopen(filename,'w');

% Was the open successful?if fid > 0 % Write the output data. count = fwrite(fid,out_array,'float64'); disp([int2str(count) ' values written...']); % Close the file status = fclose(fid);else % Output file open failed. Display message. disp(msg);end

Page 7: Fprintf and other examples. Save command >> !ls >> a = 3; >> save afile a >> !ls afile.mat >> !dir Directory of C:\MATLAB6p5\work\save 11/21/2003 09:30

fopen

fid = fopen(filename,permission) opens the file filename in the mode specified by permission. permission can be:

'r‘ Open file for reading (default).

'w‘ Open file, or create new file, for writing; discard existing contents, if any.

'a‘ Open file, or create new file, for writing; append data to the end of the file.

'r+‘ Open file for reading and writing.

'w+‘ Open file, or create a new file, for reading and writing; discard existing contents, if any.

'a+‘ Open file, or create new file, for reading and writing; append data to the

end of the file.

Page 8: Fprintf and other examples. Save command >> !ls >> a = 3; >> save afile a >> !ls afile.mat >> !dir Directory of C:\MATLAB6p5\work\save 11/21/2003 09:30

Reading Data Back

% Now try to recover the data. Open the file for reading.[fid,msg] = fopen(filename,'r');if fid > 0 % Read the input data. [in_array, count] = fread(fid,[100 100],'float64'); disp([int2str(count) ' values read...']); % Close the file status = fclose(fid);else % Input file open failed. Display message. disp(msg);end

Page 9: Fprintf and other examples. Save command >> !ls >> a = 3; >> save afile a >> !ls afile.mat >> !dir Directory of C:\MATLAB6p5\work\save 11/21/2003 09:30

fread function

[A, COUNT] = FREAD(FID,SIZE,PRECISION) reads binary data from the specified file and writes it into matrix A. Optional output argument COUNT returns the number of elements successfully read.

FID is an integer file identifier obtained from FOPEN. The SIZE argument is optional; if not specified, the entire file is read; if specified, valid entries are: N read N elements into a column vector. inf read to the end of the file. [M,N] read elements to fill an M-by-N matrix, in column

order. N can be inf, but M can't.

Page 10: Fprintf and other examples. Save command >> !ls >> a = 3; >> save afile a >> !ls afile.mat >> !dir Directory of C:\MATLAB6p5\work\save 11/21/2003 09:30

fprintf example

>> fid = fopen('grades', 'w');

>> fprintf(fid, '%d', randperm(40));

>> fclose(fid);

>> !more grades

24318212515273039262292216193610337354382831140143261223937520181334171

Page 11: Fprintf and other examples. Save command >> !ls >> a = 3; >> save afile a >> !ls afile.mat >> !dir Directory of C:\MATLAB6p5\work\save 11/21/2003 09:30

fprintf example

>> fid = fopen('grades', 'w');

>> fprintf(fid, '%d ', randperm(40));

>> fclose(fid);

>> !more grades

2 15 10 11 28 13 9 32 29 27 35 17 4 8 38 21 6 30 14 37 34 20 40 24 26 3 12 39 16 36 7 31 25 23 5 1 33 19 18 22

Page 12: Fprintf and other examples. Save command >> !ls >> a = 3; >> save afile a >> !ls afile.mat >> !dir Directory of C:\MATLAB6p5\work\save 11/21/2003 09:30

fscanf example

>> fid = fopen('grades', 'r');>> grs = fscanf(fid, '%d', [1 40])grs = Columns 1 through 13

2 15 10 11 28 13 9 32 29 27 35 17 4

…>> grs = fscanf(fid, '%d', [5 4])grs = []