MATLAB: How to import binary data or a text file into the MATLAB workspace

binarydataimportintoMATLABwizardworkspace

I would like to know how to import binary data or a text file into the MATLAB workspace.

Best Answer

The easiest way to import binary data is by using the Import Wizard. This feature is available in MATLAB 6.0 (Release 12) and releases after that. You may start the Import Wizard by entering the following command at the command prompt:
uiimport
and specify the file that contains the data you want to import by clicking on the "browse" button. For more information, type 'doc' at the MATLAB command prompt and type 'import wizard' into the 'search' tab (if you have the documentation installed).
If you need to work from the MATLAB command line or perform import operations as part of a MATLAB function, you must use one of the MATLAB import functions. MATLAB supports many functions to import data in different binary formats, such as image files or spreadsheet data files. Your choice of which function to use depends on the type of data in the file and how the data is formatted. For example, type 'help fread' at the MATLAB command prompt for more information.
To import text data using the Import Wizard, perform these steps:
1) Start the Import Wizard, by selecting the Import Data option on the MATLAB File menu. MATLAB displays a file selection dialog box. You can also use the UIIMPORT function to start the Import Wizard. In the Import Wizard you have the option of selecting a file to import or you can import data from the clipboard.
Skip to step 3 to continue importing from the clipboard.
2) Specify the file you want to import in the file selection dialog box and click Open. The Import Wizard opens the file and attempts to process its contents. See "Viewing the variables" for more information.
3) Select the variables that you want to import. By default, the Import Wizard creates variables depending on the type of data in the file.
4) Click Finish to import the data into the workspace.
Viewing the Variables:
When the Import Wizard opens a binary data file, it attempts to process the data in the file, creating variables from the data it finds in the file.
For example, if you use the Import Wizard to import this sample MAT-file, my_data.mat,
A =
1 2 3 4 5
6 7 8 9 10
B =
a test string
it creates two variables, listed in the Preview pane. You can select the variables you want to import by clicking in the check box next to its name. All variables are preselected by default. All of this information, plus more, is located in the HELP documentation and can be found by typing 'doc' at the MATLAB command prompt.
To import text data using the Import Wizard, perform these steps:
1) Start the Import Wizard, by selecting the Import Data option on the MATLAB File menu. MATLAB displays a file selection dialog box. You can also use the UIIMPORT function to start the Import Wizard.
Skip to step 3 to continue importing from the clipboard.
2) Specify the file you want to import in the file selection dialog box and click Open. The Import Wizard opens the file and attempts to process its contents.
3) Specify the character used to separate the individual data items. This character is called the delimiter or column-separator. The Import Wizard can determine the delimiter used in many cases. However, you may need to specify the character used in your text file. See Specifying the Delimiter for more information. Once the Import Wizard has correctly processed the data, click Next.
4) Select the variables that you want to import. By default, the Import Wizard puts all the numeric data in one variable and all the text data in other variables but you can choose other options. See Selecting the Variables to Import for more information.
5) Click Finish to import the data into the workspace.
Specifying the Delimiter:
When the Import Wizard opens a text file, or copies data from the clipboard, it displays a portion of the raw data in the Preview pane of the dialog box. You can use this display to verify that the file contains the data you expected.
The Import Wizard also attempts to process the data, identifying the delimiter used in the data. The Import Wizard displays the variables it has created based on its interpretation of the delimiter, using tabbed panels to display multiple variables.
For example, in the following figure, the Import Wizard has opened this sample file, grades.txt.
John 85 90 95
Ann 90 92 98
Martin 100 95 97
Rob 77 86 93
Also, there are other related functions which may be applicable depending on the requirement, like:
fscanf, textread etc.
The details about the above functions can be found in the doc:
doc textread
doc fscanf