Matlab Tutorial 4: Data Analysis and Statistics with Matlab


As you well can imagine now these formatting codes are very useful and there many others that can be used together with the fprintf command , but there some other commands that use the same formatting codes. Like fscanf can be used to read from text files or textscan that also reads text and converts this to a cell array. See the table below for several format codes.

String Formatting Codes

  • n New line
  • r Carriage return
  • b Backspace
  • t Horizontal tab
  • f Form feed
  • %s String of characters
  • %e Exponential notation
  • %f Fixed-point notation
  • %u Decimal notation (unsigned)
  • %g The more compact of %e or %f

On the homepage we have a text file: name.txt. We shall now try to read with the use of command textscan.

>> fid=fopen('name.txt','r'); % opens the file for reading.
>> C=textscan(fid,'%u%s%u%u'); % Gives a cell array.
>> fclose(fid) % closes the file for reading.

Take a look on the cell array C!

>> C{1,1},C{1,2},C{1,3},C{1,4}

As I have said earlier there are other commands that can do this equally well. Use the Matlab help to find out what other possibilities there are to solve this.

Related posts:

  1. Matlab Tutorial 2: Matrices in Matlab
  2. Matlab Tutorial 1: Hello world, plotting, mathematical functions and file types
  3. Matlab Tutorial 3: Strings in Matlab
  4. Matlab Tutorial 6: Analysis of Functions, Interpolation, Curve Fitting, Integrals and Differential Equations

Pages: 1 2 3

No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment