Download presentation
Presentation is loading. Please wait.
Published byPrimrose Harvey Modified over 6 years ago
1
Working with files Saving and loading Matlab variables to and from .mat files does not require any special file handling, just use save() and load() However, if you want to read another kind of file, or create a file that can be read outside of Matlab, you must deal with files directly Matlab can also handle text files and and binary files (as well as excel files – see xlsread and xlswrite)
2
Working with files Introducing fopen() and fclose()
not always necessary but… … general plan for working with files: >> help fopen % check bin vs. text mode & corresponding permission codes fopen() <read from file or write to file> fclose()
3
Opening files fid = fopen(filename, permission)
number returned by fopen which you will use to refer to this file fid = fopen(filename, permission) string with name of file or full path to file if it’s not in the current directory string containing code that determines what Matlab is allowed to do with this file
4
Writing to text files >> myFileID = fopen('testfile.txt','w') myFileID = 3 >> x = 100; >> fprintf(myFileID,'X is equal to %d\n',x); >> fclose(myFileID); >> fopen('/usr/bin/test.txt','w') ans = -1 If fopen() returns -1 then it has failed to open the file >> x = [1:10]; >> y = x .^3; >> myExponentsFile = fopen('e.txt','w'); >> fprintf(myExponentsFile,'%d %d\n',[x;y]); >> fclose(myExponentsFile)
5
Writing to text files Other ways to write (matrices) to files:
csvwrite() % write a comma-separated value file. dlmwrite() % write ASCII delimited file data=rand(10,5); dlmwrite('data1.csv',data); dlmwrite('data2.tab',data,'\t'); >> x = rand(5) x = >> csvwrite('randomvalues.csv',x) >> clear all >> x = csvread('randomvalues.csv')
6
Reading from text files
to read numeric data from a text file: load(‘filename’) % can also write such data with save csvread(‘filename’) % read comma separated file dlmread(‘filename’) % read ASCII delimited file to read strings from a text file: fgets(fid) % used with files that contain newline characters fgetl(fid) % here the line terminator is not included note: these commands read one line only so use ischar in a while loop to read the whole text To read both or even mixed stuff from a text file: importdata(‘filename’) % can have non-numeric headers A= fscanf(fid,FORMAT) % can also determine range/shape (works columnwise!) textscan(fid, FORMAT) % reads data into a cell array >> help type % prints a text file in the command window
7
Reading from text files - textscan
>> help textscan textscan Read formatted data from text file or string. C = textscan(FID,'FORMAT') reads data from an open text file identified by FID into cell array C. Use FOPEN to open the file and obtain FID. The FORMAT is a string of conversion specifiers enclosed in single quotation marks. The number of specifiers determines the number of cells in the cell array C. For more information, see "Format Options."
8
Reading from text files - textscan
Contents of "log.txt": >> logFID = fopen('log.txt'); >> data = textscan(logFID,'%s %f %f %f %f %f %f') data = Columns 1 through 5 {9x1 cell} [9x1 double] [9x1 double] [9x1 double] [9x1 double] Columns 6 through 7 [9x1 double] [9x1 double]
9
Reading from text files - textscan
>> subjectcodes = data{1} subjectcodes = 'SM01' 'SM02' 'SM03' 'SM04' 'SM05' 'SM06' 'SM07' 'SM08' 'SM09' >> fclose(logFID); >>
10
Binary-files (data files, e.g. ‘.dat’)
to write an array A into a binary file: fid=fopen(filename,’w’) fwrite(fid, A ,’double’) % can have other types, including ‘char’ fclose(fid) help fwrite to read an array A from a binary file: fid=fopen(filename,’r’) fread(fid, inf, ‘double’) % inf means read all - read/write range can be specified help fread problem: fwrite columnwise produces a single column so if I want to retrieve my original matrix, I need to include its dimensions inside the file (first the #dims, then the dims themselves and then the actual data).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.