1 / 12

fprintf and other examples

fprintf and other examples. Save command. >> !ls >> a = 3; >> save afile a >> !ls afile.mat >> !dir Directory of C:MATLAB6p5worksave 11/21/2003 09:30 AM <DIR> . 11/21/2003 09:30 AM <DIR> .. 11/21/2003 09:30 AM 184 afile.mat

harris
Télécharger la présentation

fprintf and other examples

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. fprintf and other examples

  2. Save command >> !ls >> a = 3; >> save afile a >> !ls afile.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

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

  4. Load/save entire workspace >> v = 1:5; >> m = ones(2, 3); >> who Your variables are: a m v >> save allfile >> !ls afile.mat allfile.mat >> clear >> whos >> load allfile.mat >> who Your variables are: a m v

  5. 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 ==> load Number of columns on line 2 of ASCII file C:\MATLAB6p5\work\save\allfile.dat must be the same as previous lines. >> who >> m = ones(2, 3); >> save -ascii m.dat >> clear >> load -ascii m.dat >> m m = 1 1 1 1 1 1

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

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

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

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

  10. fprintf example >> fid = fopen('grades', 'w'); >> fprintf(fid, '%d', randperm(40)); >> fclose(fid); >> !more grades 24318212515273039262292216193610337354382831140143261223937520181334171

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

  12. 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 = []

More Related