MATLAB data import

Source: Internet
Author: User
Tags numeric require

MATLAB imports data in 2 ways, one is in the command line through the code to the data in, the other is through the Data Import Wizard matlab import. The following distributions describe both of these approaches:

First, using the MATLAB Data Import Wizard

1, first look at the TXT document to save the data structure, as shown in the figure, the data separated by a space, this structure is more ideal. can be imported directly. or the data with commas, semicolons, tab symbols, and so on, are counted as more ideal.

2, then we open matlab, under the File menu to find the Import data option, select the TXT to be added. Add in.

3, then the Data Import Setup Wizard interface appears, we first want to choose what symbol separates the data, here automatically use a space as a spacer, you can also use comma (comma), semicolon (semicolon), tab (four spaces), and other symbols. Here we choose to be a space, because the data in TXT is separated by a space.

4. Choose a data format, such as a column vector or a numerical matrix, according to the format you want for your data. Here we choose the data matrix, and we see that the data is stored in a matrix. There are no column headings and row headings.

5, select all the data and click Import, we see the data into the MATLAB workspace, in the right side of the work area can see the numerical matrix.

6. By right-clicking the value matrix of the workspace, you can see the operation of this matrix, select Save As can put this. mat file to the location we want, in general, and our own items in a folder, can also be modified. The name of the mat

Second, import data through the instruction. such as text files, Word files, XML files, XLS files, image files, and audio and video files. This section describes reading and writing text files (TXT). Read and write other files, the user can refer to MATLAB help documentation.

1. The calling format of the Csvread function is as follows:

M = csvread (' filename '), the data in the file filename is read into, and saved to M,filename can only contain numbers, and the numbers are separated by commas. M is an array with the same number of rows as the filename and the maximum number of columns for the filename column, supplemented by 0 for rows with insufficient elements.

M = csvread (' filename ', row, col), read the data in file filename, start with row, start with Col, it should be noted that at this point the ranks start at 0.

M = csvread (' filename ', row, col, range), read the data in file filename, start with row, start column col, read data is specified by array range, the format of range is: [R1 C1 R2 C2], its R1, C1 are the rows and columns in the upper-left corner of the reading area, R2, C2 are the rows and columns in the lower-right corner of the read area.

The call format for the Csvwrite function is as follows:

Csvwrite (' filename ', m), saves the data in the array m as a filename and separates the data with commas.

Csvwrite (' filename ', m,row,col), saves the specified data in the array M in the file, the data is specified by the parameter row and col, and the data in the lower right corner of the row and Col is saved.

Csvwrite each line ends with a newline character when writing data. Additionally, the function does not return any values.

2. Excel data import directive

data = Xlsread (' A.xls '); First, put the A.xls file in the MATLAB working directory

3, for the text-only TXT file

Can be directly used X=load (' a.txt ')

Add:

There are two types of file I/O programs in MATLAB: High levels and low level.
High level routines: includes out-of-the-box functions that can be used to read and write data in special formats and require only a small amount of programming.
Low level routines: more flexible to perform relative special tasks, requiring more additional programming.

The high level routines includes out-of-the-box functions that can be used to read and write data in special formats and require only a small amount of programming.

For example, if you have a text file that contains numeric and alphabetic characters (text files) that you want to import into MATLAB, you can call some low-level routines to write a function yourself, or simply use the Textread function.

the key to using the high level routines is that the file must be similar (homogeneous), in other words, the file must have a consistent format. The following paragraphs describe some high-level file I/O routines and give some examples to help understand the concepts.

Load/save

The main high-level file I/O routines is the load and save function. LOAD
You can read mat-file data or ASCII data in a format similar to the space interval. Save can write MATLAB variables to ASCII data in mat-file format or space interval. In most cases, the syntax is fairly simple. The following example uses the ASCII file Sample_file.txt with numeric values separated by spaces:

1 5 4) 16 8

5 43 2) 6 8

6 8 4) 32 1

90 7 8) 7 6

5 9 81) 2 3

Example:
Read and write data with LOAD and SAVE


CODE:

% Load the file to the matrix, M:
M = Load (' sample_file.txt ')

% ADD 5 to M:
m = M +5

% Save M to a. mat file called ' Sample_file_plus5.mat ':
Save SAMPLE_FILE_PLUS5 M

% Save M to an ASCII. txt file called ' Sample_file_plus5.txt ':
Save Sample_file_plus5.txt M-ascii

Uigetfile/uiputfile

Uigetfile/uiputfile is based on the graphical user interface (GUI). A dialog box pops up to list the files and directories of the current directory, prompting you to select a file. Uigetfile lets you select a file to write (similar to the Windows ' Save as ' option. )。 With Uigetfile, you can select existing files to overwrite, or you can enter a new file name. The return value of two functions is the selected file name and path.

Example:
Select a m-file from the current directory with Uigetfile


CODE:

% This command lists all the m-files in the current directory and
% returns the name and path of the selected file

[Fname,pname] = uigetfile (' *.m ', ' Sample Dialog Box ')


Note: Uigetfile can only select one file at a time.

Uiimport/importdata

Uiimport is a powerful, easy-to-use GUI-based high-level routine for reading complex data files. The file must also be homogeneous.

ImportData forms the function of Uiimport and does not open the GUI. ImportData can be used in functions or scripts because the GUI-based file import mechanism in a function or script is not ideal. The following example uses the file ' Sample_file2.txt ' that contains several lines of file headers and text, numeric data:

This is a file header.

This is a file is a example.

col1 col2 col3 Col4

A 1 4 612.000

B 1 4 613.000

C 1 4 614.000

D 1 4 615.000

Example:using ImportData to read in a file with headers, text, and numeric data


CODE:

% this reads in the file ' Sample_file2.txt ' and creates a
% structure D that contains both data and text data.
% Note The importdata command specifies a white space
% as the delimiter of the file, but ImportData can usually
% detect this in its own

D = ImportData (' sample_file2.txt ', ')% of the original is incorrect.
D = ImportData (' sample_file2.txt ')

You can see the real values in structure d by accessing the data and text fields of structure d, such as input:

data = D.data

Text = D.textdata

You can use Uiimport to read the same file and get the same structure.

Note: For ASCII data, you must verify that the Import Wizard correctly recognizes the column delimiter.

Textread/strread

Textread is a powerful dynamic high-level routine designed to read ASCII-formatted text and/or numeric data files. Strread is similar to Textread except that it is read from a string rather than a file.

Two functions can use a number of parameters to change the way they work, and they return the data that is read into the specified output. They effectively provide you with a
The "Best of both worlds" approach because they can be read into mixed ASCII and numeric data with a single command (high-level routines approach), and you can change them to match your particular application (as low-level routines do). Example:


CODE:
Example 1:using Textread to read in an entire file into a cell array


% This command reads in the file fft.m into the cell array, file
File = Textread (' fft.m ', '%s ', ' delimiter ', ' \ n ', ' whitespace ', ');


CODE:
Example 2:using Strread to read the words in a line

% This command uses the cell array created

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.