Input and Output
Output
Simplest way to output is to leave off the semicolon.
Which can be fomratted with format [long|short].
Use disp to output a matrix using default formats.
- Mix values and text into one output with
num2strorint2strto convert numbers to strings. - e.g.
disp(['The value of pi is' num2str(pi)]);
sprintf can format strings without sending them to a file. Sends an an output and has the same syntax as fprintf, except no fid is required.
Input
The input command obtains a value from the user. The input will be stored as a string or number, or enforce a value with
x = input('Please enter a number.');
y = input('Please enter something.', 's');
Data to Files
Save data in the workspace by using the save command.
save filename var1 var2 var3 options
- You can select the variables you want to save, or omit variable list to save all.
- the flag
-regexpprovides a "regular expression" that determines the variables to save. E.g.,-regexp mysaves all variables with names that starts wihtmylikemylistandmynum.
Load data back into memory with the load command.
load filename options var1 var2
-asciiand-mat(default) change the file type to be saved as-appendappends to the file
Data files can come as a formatted character data which sotres in ASCII format or binary data that is directly copied from memory in the computer's native file.
- Formatted data can easily be displayed and inspected by different computers but it is a waste of space
- Binary data is not easy to display but it is faster.
Reference a particular file through a file's file ID. or fid. Obtain this ID by using fopen.
fid = fopen(filename, permission, format)
[fid, message] = fopen(filename, permission, format)
formatis optional and should only be used when transferring data between computers.- add
tif the file is going to be text andbif the file is binary fidwill be-1if there is a problem opening the file and there will be a message returned.
Close a file with fclose
status = close('all')
status = close(fid)
'all'closes every file except for standard output and inputstatusis0if there is no problem and-1if there is an error.
Formatted output with fprintf function. It returns the number of bytes written:
fprintf('The value of pi is %6.2g \n', pi );
- format specifier like
%-12.5eincludes%the marker*,-modifier,12field width (how many spaces to leave before printing the number),.5precision, andeformat descriptor*. Where any marked with * is required.
\
File to Matlab
Read a file:
C = textscan( fid, 'format', N );
where N is the number of lines to read, 'format' is a string describing the type of data in each column.
Example usage:
myf=fopen('class.data');
Cdata = textscan( myf, '%s %d');
fclose(myf);
textscan also works with parsing strings
These functions reads a line from the file.
line = fgetl(fid)
line = fgets(fid)
fgetlexcludes the end-of-line characters, whilefgetsincludes them.- If the file ends then line is returned with the value -1.
Check files
exist('item') looks for a variable, built-in function, directory or file called item and returns a code.
exist('item','kind') only looks for items of type 'kind' where this can be 'var', 'file', 'builtin', or 'dir'.
Codes:
- 0: not found
- 1: variable
- 2: M-file
- 3: MEX file
- 4: MDL file
- 5: function
- 6: P file
- 7: directory
- 8: Java Class
[message, errnum] = ferror(fid) returns the error message and error number.
- if there are no errors, then
errnumis0and themessageis'..'.errnumoutput is optional.
eofstat = feof(fid) checks if you have reached the end of the file. 0 means the end file has not been reached while 1 means you have reached the end of the file.
Positioning
Controlling where you are reading and writing:
position=ftell(fid) determines the position in bytes
frewind(fid) resets the file's position to the beginning
status = fseek(fid, offset, origin) repositions the indicator to the offset byte location relative to the origin.
origin can be bof beginning of file, cof current position, or eof end of file.
Linked from Computational Modeling