Input and Output

Created


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 num2str or int2str to 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 -regexp provides a "regular expression" that determines the variables to save. E.g., -regexp my saves all variables with names that starts wiht my like mylist and mynum.


Load data back into memory with the load command.

load filename options var1 var2
  • -ascii and -mat (default) change the file type to be saved as
  • -append appends 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)
  • format is optional and should only be used when transferring data between computers.
  • add t if the file is going to be text and b if the file is binary
  • fid will be -1 if 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 input
  • status is 0 if there is no problem and -1 if 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.5e includes % the marker*, - modifier, 12 field width (how many spaces to leave before printing the number), .5 precision, and e format 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)
  • fgetl excludes the end-of-line characters, while fgetsincludes 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 errnum is 0 and the message is '..'. errnum output 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.