SECTION I.4. ADVANCED IMPORTING AND EXPORTING TECHNIQUES

SUMMARY

  1. Importing data from a file
  2. Long lines of data
  3. Importing data from the keyboard; using the input function
  4. Saving variables to ".mat" files
  5. Reloading variables from ".mat" files
  6. Saving one variable in ASCII format

 

[Typical names of files are indicated in the summary by italics; note that in the save and load commands discussed in iv and v, the extension .mat is added automatically by MATLAB. The expression "m-file" refers to a file with the extension *.m]

  1. Importing data.

    Use a word editor to create an "m-file" e.g. import1.m in which you have entered the variables and data exactly as you would at the MATLAB command line.

  2. Long lines of data.

    The data is enclosed in brackets [ ] as is the case with all matrices of data. At the end of each line we type ellipses ... , push ENTER, then continue the data on the next line. This applies to "m-files" as well as at the MATLAB command.

    [Warning! Leave a space after the last number, e.g. 5 ... Otherwise MATLAB thinks that you have put decimal points.]

  3. Importing data from the keyboard.

    vect_1 = input( ' type in your first vector: ');

    for string variables use the form:
    comment = input(' type your comment: ' , 's' );

    [Note: A string input is made without apostrophes, e.g., yes (not 'yes').]

  4. Saving variables to a "mat-file" called results.mat.

    To save all variables: save results (no extension)
    To save several specific variable: save results A1 a

    [Note: there is no comma anyplace in the statement.

  5. Reloading variables from a "mat-file" results.mat.

    >> load results

  6. Saving one variable A in ascii format to a file data1.

    >> save data1 A /ascii /double

[Note and warning: As discussed in section I.1 output can be saved in a "diary". Although the output from such a diary can be placed (by use of a text editor) in an m-file and then loaded into MATLAB, this is not advisable when decimals are present and high accuracy is desired. A variable saved in a mat-file is saved to a high degree of accuracy, whereas a variable saved in a diary has been permanently truncated!

ASCII output (see vi) can only be done one variable at a time and the accuracy may not be as high as with mat-files.]

 

DETAILS AND EXAMPLES

  1. Importing data.

    For small data sets one can enter the numbers at the MATLAB control line. For larger sets of data and for situations where one wants to make repeated changes, it is better to use a text editor to create an ASCII file which contains the data. When naming the file, the name is arbitrary, but the extension must be ".m", e.g. data1.m. To call the program one types the name, e.g. data of the file, without the extension.

    The concept of m-files was discussed in section I.3.i in connection with programs. From the viewpoint of MATLAB a program file is no different from a data file. The concept of putting data in an m-file is repeated here so that a comparison can be made with other methods discussed later in this section. Recall also that in section I.3.iii, we saw how user defined functions were also created by putting the instructions in a file with the extension .m . The difference between function files and program/data m-files is that a function file starts off with the word "function". Functions, via function files, operate on variables, whereas m-files contain a once only sequence of instructions.

    Example 1. A simple data file.

    The file contains the names of two variables: a matrix and a
    scalar, and the corresponding data. Note that the variable name
    MAT1 is not the same as the file name. However it could be the same if
    we wished.

    Program 1

     

  2. % [import.m]
    MAT1 = [1 2 3; 4 5 6]; % the semicolon suppresses printing
    a = sin( pi/2 ); % sin( ) and pi are MATLAB functions

     

     

    To run this program in MATLAB we simply type the name of the file, without the extension, at the command line.

    >> import1

    To check that the variables are there:

    >> whos

    Name
    Size
    Total
    Complex
    MAT1
    2 by 3
    6
    No
    a
    1 by 1
    1
    No

     

  3. Long lines of data.

    For long strings of data we want to be able to continue data entry on the next line. To do so, type three or more ellipses ... , push ENTER and continue the data on the next line. Make sure that you put a semi-colon if you do not want a huge mass of data to be printed out!

    Ellipses can be used in "m-files" as well as at the MATLAB command.

    Program 2

    % [dots.m]

    % place the data in brackets; add ellipses at the end
    % of each line of data. But make sure that you leave a space
    % between the last number and the ellipses.
    % place a semicolon after the data to suppress printing.
    % there is no need to have the same number of elements in each row.

    a = [1 3 ...

  4. 4 ];

    s = sum(a)

     

    >> dots % call up the program "dots.m"; no extension

    a =

    1 3 4


    s =

    8

    If we have large sets of data and want to have the same number of elements in each row we can place ... in the first row.

    a = [ ... % first row is empty
    * * * ... % !! space after the last
    number
    * * * ...
    ]; % ! closing bracket

    [Warning: Leave a space between the last number and the ellipses; otherwise MATLAB thinks that you have put a decimal point!]

    [Note: Nothing prevents you from using a semicolon to form a
    matrix. For example:

    >> F = [4 5 6 ...   % ellipses to continue row 1
    7 8 ;
      % end of row 1 indicated by a
    semicolon  
    9 10 11 12 13]
      % row 2 all on one line

    F =

    4 5 6 7 8
    9 10 11 12 13

  5. Inputing data from the keyboard.

    One can also import data directly from the keyboard. This is especially useful in connection with a program that we want to check or run several times and which involves small sized matrices. By using the keyboard, we can avoid switching to a text editor in order to change the data.

    To invoke the keyboard, we assign a variable, e.g. vect_1 to the MATLAB function input(' '). Inside the apostrophes, we place a prompt message:

    >> vect_1 = input( ' type in your first vector: ' )

    Note that for clarity a colon has been included in the prompt message and a little space has been left after the colon.

    The input can also be a string, i.e. text, and to indicate this we add , 's' :

    >> comment = input( ' type your comment: ' , 's');

    When we input the text we do not put the apostrophes around it; i.e if the input asks yes or no? we type yes, not 'yes'.

    Example 2. Program 3 illustrates the use of input(' ') to input two variables and to input a comment.

    Note that I have not placed a semicolon after vect_1 and vect_2 and so MATLAB prints out the values. I could have placed a semicolon and then used disp(vect_1). This however has been done with the variable "comment"; a semicolon has been placed at the end of the line which defines "comment" and then disp( comment ) has been used.

    The example shows that when we multiply a column vector by a row vector we obtain a matrix of rank 1. We input vect_1 as a row vector and then take its transpose by using the true transpose operator .' ; see section II.3.i for a discussion of the transpose operators.

    Program 3

    % [keyb1.m]

    vect_1 = input(' type in your first vector: ')
    vect_2 = input(' type in your second vector: ')

    % multiply the transpose of vect_1 and vect_2; find the rank
    % .' is the true transpose; see section II.3.i

    col_x_row = (vect_1).' * (vect_2);
    ran_1 = rank(col_x_row) ;

    comment = input(' type your comment: ', 's');
    disp(comment)

    Invoke this program by typing the file name keyb1 without the extension. Boldface in the following indicates what the prompt text is. Italics in the following indicates what is typed in at the keyboard.

    >> keyb1

    type in your first vector: [1 2]

    vect_1 =

    1 2

    type in your second vector: [3 4]

    vect_2 =

    3 4

    col_x_row =

    3 4
    6 8

    ran_1 =

    1

    type your comment: each row is a multiple of first

    each row is a multiple of first

  6. Saving variables to "mat-files" and reloading them.

  7. In example 2 we imported two variables vect_1 and vect_2, and then calculated the values of two variables col_x_row and ran_1. If we wanted to save all four variables for later use, the simplest and best manner is to save them to a "mat-file". This is a file written in machine language in which the variables are saved to a very high degree of accuracy. In principle, one could take the contents of a "diary" that has been created (section I.1), put them into an "m-file" (as in subsection i above) and then call up the "m-file". Not only would that way require a lot of work, but it also has the added inconvenience that accuracy is lost because the output appears in truncated form in the "diary". If you need to save the variables in ascii format, e.g. for export to another program, see vi below and the save ... ascii command; the precision may be less however.

    To place all the current variables into a "mat-file" called results we give the command save and follow this by the name (without the extension ".mat")

    >> save results

    MATLAB will automatically assign the extension .mat so that in the directory there will be a file results.mat.

    If during another MATLAB session we want to have access to the variables we use the load command:

    >> load results

    Sometimes we only want to save a few of the variables. Supose that we only wanted to save vect_1 and col_x_row to a mat-file results2.mat. We could either first clear vec_2 and ran_1 by using:

    >> clear vec_2, ran_1

    and then typing:

    >> save results2

    or we could use type save followed the name of the file results2 and then list the variables that we want to save. Notice that there is no comma anyplace in the statement.

     

    >> save results2 vec_1 ran_1 % savevec_1 and ran_1 to results2

    To re-obtain the variables from results2, we use the load command:

    >> load results2

    To find what variables were stored in results2, and what their dimensions are, we use the command whos:

    >> whos

    Name
    Size
    Total
    Complex
    vec_1
    1 by 2
    2
    No
    ran_1a
    1 by 1
    1
    No

    We can now display the variables that we want by typing their names or by using the command disp( ):

    >> vec_1 % "vec_1 =" is printed
    >> disp(ran_1a) % only the value is printed

    Note: It is not possible to load just one of the variables from the file tresults. Just load and clear the ones that you do not want.

  8. Saving one variable A in ascii format to a file data1.

To save the variable "A" in ascii format to a file data1, we use save with the option /ascii:

>> save data1 A /ascii /double.

The option /double is usually added to ensure a high degree of accuracy.

Top

Back to the Table of Contents

Web Site Training provided by Kathryn Charis Apunen Washburn summer 2001