English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Importing data in MATLAB means loading data from external files. TheimportdataThe function allows loading various data files in different formats. It has the following five forms-
Serial Number | Function Description |
---|---|
1 | A = importdata(filename) fromfilenamerepresentsFileLoad the data into the array A. |
2 | A = importdata('-pastespecial') Load data from the system clipboard instead of a file. |
3 | A = importdata(___, delimiterIn) Interpret delimiterIn as the column delimiter in ASCII files, file names, or clipboard data. delimiterIn can be used for any input parameter in the above syntax. |
4 | A = importdata(___, delimiterIn, headerlinesIn) Load data from an ASCII file, file name, or clipboard, and from the lineheaderlinesIn + 1Start reading numeric data. |
5 | [A, delimiterOut, headerlinesOut] = importdata(___) Using any input parameters from the previous syntax, returns the detected delimiter character in delimiterOut and the number of detected header lines in headerlinesOut. |
By default, Octave does not support theimportdata()Feature, so you must search and install this software package to make the following example applicable to Octave installation.
Let's load and display the image file. Create a script file and enter the following code-
filename = 'smile.jpg'; A = importdata(filename); image(A);
When running the file, MATLAB will display the image file. However, it must be stored in the current directory.
In this example, we import a text file and specify Delimiter and Column Header. Let's create a space-separated ASCII file with headersThe weeklydata.txtColumn headers.
Our text file weeklydata.txt looks like this-
SunDay MonDay TuesDay WednesDay ThursDay FriDay SaturDay 95.01 76.21 61.54 40.57 55.79 70.28 81.53 73.11 45.65 79.19 93.55 75.29 69.87 74.68 60.68 41.85 92.18 91.69 81.32 90.38 74.51 48.60 82.14 73.82 41.03 0.99 67.22 93.18 89.13 44.47 57.63 89.36 13.89 19.88 46.60
Create a script file and enter the following code-
filename = 'weeklydata.txt'; delimiterIn = ' '; headerlinesIn = 1; A = importdata(filename,delimiterIn,headerlinesIn); % View data for k = [1:7] disp(A.colheaders{1, k) disp(A.data(:, k)) disp(' ') end
When the file is executed, it displays the following result-
SunDay 95.0100 73.1100 60.6800 48.6000 89.1300 MonDay 76.2100 45.6500 41.8500 82.1400 44.4700 TuesDay 61.5400 79.1900 92.1800 73.8200 57.6300 WednesDay 40.5700 93.5500 91.6900 41.0300 89.3600 ThursDay 55.7900 75.2900 81.3200 0.9900 13.8900 FriDay 70.2800 69.8700 90.3800 67.2200 19.8800 SaturDay 81.5300 74.6800 74.5100 93.1800 46.6000
In this example, let's import data from the clipboard.
Copy the following line to the clipboard-
Mathematics is simple
Create a script file and enter the following code-
A = importdata('-pastespecial')
When the file is executed, it displays the following result-
A = 'Mathematics is simple'
The importdata function is an advanced function. The underlying file I/The O function allows for maximum control over file read and write data. However, these functions require more detailed file information to work effectively.
MATLAB为字节或字符级别的读写操作提供以下函数-
函数 | MATLAB provides the following functions for byte or character level read and write operations |
---|---|
Function | Description |
fclose | Close one or all open files |
feof | File end test/ferror |
Functions read formatted data from text or ASCII files. | About file I |
and | O error information |
Using | Read lines from the file, removing the newline character |
Read lines from the file, retaining the newline character | Open a file or get information about the opened file |
The time, where each line separated by a newline character is a line read by the function. | fprintf |
Read data from a binary file | frewind |
By default, | Move the file pointer to the beginning of the opened file |
Read data from a text file | fseek |
Move to the specified position in the file | ftell |
The position in the opened file | fwrite |
O's text data file-
By default,MATLAB provides the following functions for importing text data files at the low level
Functions read formatted data from text or ASCII files.fgetlandfgets
The time, where each line separated by a newline character is a line read by the function.fread
Online examples2012We saved a text data file "myfile.txt" in the working directory. The file stores rainfall data for three months;
June, July and August of the year.
myfile.txt data file contains a set of repeated measurements of time, month and rainfall for five locations. The header data stores the number of months M; therefore, we have M sets of measurements.-
The file looks like this Rainfall Data Months: June, July, August 3 12:00:00 June-2012 17.21 28.52 39.78 16.55 23.67 19.15 0.35 17.57 .00 12.01 17.92 28.49 17.40 17.06 11.09 9.59 9.33 M =31 0.23 10.46 13.17 .00 14.89 19.33 20.97 19.50 17.65 14.45 14NaN 0. 18.23 10.34 17.95 16.46 19.34 09:10:02 July-2012 12.76 16.94 14.38 11.86 16.89 20.46 23.17 .00 24.89 19.33 30.97 49.50 47.65 24.45 34NaN 0. 18.23 30.34 27.95 16.46 19.34 30.46 33.17 .00 34.89 29.33 30.97 49.50 47.65 24.45 34NaN 0. 28.67 30.34 27.95 36.46 29.34 15:03:40 August-2012 17.09 16.55 19.59 17.25 19.22 17.54 11.45 13.48 22.55 24.01 .00 21.19 25.85 25.05 27.21 26.79 24.98 12.23 16.99 18.67 17.54 11.45 13.48 22.55 24.01 .00 21.19 25.85 25.05 27.21 26.79 24.98 12.23 16.99 18.67
NaN-
We will import data from this file and display this data. Take the following stepsUsingfopen
The function opens the file and gets the file identifier.used to describe the data in the fileFormat specifiers For example,%s ' indicates a string,%d ' indicates an integer, or%f
' indicates a floating-point number.*To skip the text characters in the file, include them in the format description. To skip the data fields, use an asterisk (*) in the specifier.
)。-
M = fscanf(fid, '%*s %*s\n%*s %*s %*s %*s\nM=%d\n\n' 1);
For example, to read the header and return a single value of M, we write like thisBy default,fscanf3According to our format description to read data until there is no content matching the data or reach the end of the file. Here, we will use the for loop to read7group data, and read the data each time5rows and
.We will create a named column in the workspacemydata-The structure of the time, used to store data read from the file. This structure has three fields, month, and raindataArray.
Create a script file and enter the following code-
filename = '/data/myfile.txt'; rows = 7; cols = 5; %Open the file fid = fopen(filename); %Read the file header to find M (number of months) M = fscanf(fid, '%*s %*s\n%*s %*s %*s %*s\nM=%d\n\n' 1); %Read each set of measurement data for n = 1:M mydata(n).time = fscanf(fid, '%s', 1); mydata(n).month = fscanf(fid, '%s', 1); %fscanf fills the array in column order %Transpose the result mydata(n).raindata = ... fscanf(fid, '%f', [rows, cols]); end for n = 1:M disp(mydata(n).time), disp(mydata(n).month) disp(mydata(n).raindata) end %Close the file fclose(fid);
When the file is executed, it displays the following result-
12:00:00 June-2012 17.2100 17.5700 11.0900 13.1700 14.4500 28.5200 NaN 9.5900 NaN 14.0000 39.7800 12.0100 9.3300 14.8900 18.2300 16.5500 17.9200 NaN 19.3300 10.3400 23.6700 28.4900 0.3100 20.9700 17.9500 19.1500 17.4000 0.2300 19.5000 16.4600 0.3500 17.0600 10.4600 17.6500 19.3400 09:10:02 July-2012 12.7600 NaN 34.0000 33.1700 24.4500 16.9400 24.8900 18.2300 NaN 34.0000 14.3800 19.3300 30.3400 34.8900 28.6700 11.8600 30.9700 27.9500 29.3300 30.3400 16.8900 49.5000 16.4600 30.9700 27.9500 20.4600 47.6500 19.3400 49.5000 36.4600 23.1700 24.4500 30.4600 47.6500 29.3400 15:03:40 August-2012 17.0900 13.4800 27.2100 11.4500 25.0500 16.5500 22.5500 26.7900 13.4800 27.2100 19.5900 24.0100 24.9800 22.5500 26.7900 17.2500 NaN 12.2300 24.0100 24.9800 19.2200 21.1900 16.9900 NaN 12.2300 17.5400 25.8500 18.6700 21.1900 16.9900 11.4500 25.0500 17.5400 25.8500 18.6700