External Interfaces |
Importing Data to MATLAB
You can introduce data from other programs into MATLAB by several methods. The best method for importing data depends on how much data there is, whether the data is already in machine-readable form, and what format the data is in. Here are some choices. Select the one that best meets your needs.
If you have a small amount of data, less than 10-15 elements, it is easy to type the data explicitly using brackets [ ]. This method is awkward for larger amounts of data because you can't edit your input if you make a mistake.
Use your text editor to create an M-file that enters your data as an explicit list of elements. This method is useful when the data isn't already in computer-readable form and you have to type it in. Essentially the same as the first method, this method has the advantage of allowing you to use your editor to change the data and correct mistakes. You can then just rerun your M-file to re-enter the data.
A flat file stores the data in ASCII form, with fixed-length rows terminated with new lines (carriage returns) and with spaces separating the numbers. You can edit ASCII flat files using a normal text editor. Flat files can be read directly into MATLAB using the load
command. The result is to create a variable with the same name as the filename.
See the load
function reference page for more information.
You can read data using fopen
, fread
, and MATLAB other low-level I/O functions. This method is useful for loading data files from other applications that have their own established file formats.
This is the method of choice if subroutines are already available for reading data files from other applications. See the section, Introducing MEX-Files, for more information.
You can write a program in C or Fortran to translate your data into MAT-file format. You can then read the MAT-file into MATLAB using the load
command. Refer to the section, Reading and Writing MAT-Files, for more information.
Using MAT-Files | Exporting Data from MATLAB |
© 1994-2005 The MathWorks, Inc.