MATLAB数据导入 - Matlab教程
在MATLAB中导入数据意味着从外部文件加载数据。importdata 函数允许加载各种数据的不同格式的文件。它有以下五种形式:
S.N. | 函数&说明 |
---|---|
1 | A = importdata(filename) Loads data into array A from the file denoted by filename. |
2 | A = importdata('-pastespecial') Loads data from the system clipboard rather than from a file. |
3 | A = importdata(_, delimiterIn) Interprets delimiterIn as the column separator in ASCII file, filename, or the clipboard data. You can use delimiterIn with any of the input arguments in the above syntaxes. |
4 | A = importdata(_, delimiterIn, headerlinesIn) Loads data from ASCII file, filename, or the clipboard, reading numeric data starting from lineheaderlinesIn+1. |
5 | [A, delimiterOut, headerlinesOut] = importdata(_) dditionally returns the detected delimiter character for the input ASCII file in delimiterOut and the detected number of header lines in headerlinesOut, using any of the input arguments in the previous syntaxes. |
> 默认情况下,Octave 没有importdata() 函数的支持,所以要搜索并安装这个包下面的例子Octave 安装工作。
示例 1
让我们加载和显示图像文件。创建一个脚本文件,并键入下面的代码:
filename = 'smile.jpg';
A = importdata(filename);
image(A);
当您运行该文件,MATLAB显示的图像文件。但是,必须保存在当前目录。
示例 2
在这个例子中,我们导入的文本文件,并指定分隔符和列标题。我们创建以空格分隔的ASCII文件的列标题,名为 weeklydata.txt。
我们的文本文件weeklydata.txt 看起来像这样:
SunDay MonDay TuesDay WednesDay ThursDay FriDay SatureDay
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
创建一个脚本文件,并键入下面的代码:
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
当您运行该文件,它会显示以下结果:
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
SatureDay
81.5300
74.6800
74.5100
93.1800
46.6000
示例 3
在这个例子中,让我们从剪贴板导入数据。
复制到剪贴板中的以下几行:
Mathematics is simple
创建一个脚本文件,并输入下面的代码:
A = importdata('-pastespecial')
当您运行该文件,它会显示以下结果:
A =
'Mathematics is simple'
低级别的文件I / O
有关importdata 函数是一个高层次的函数。低级别的文件,在MATLAB中的I / O功能允许读取或写入数据到一个文件中的大部分控制权。然而,这些功能都需要您的文件更详细的信息,以提高工作效率。
MATLAB字节或字符的读取和写入操作提供了以下功能:
函数 | 描述 |
---|---|
fclose | Close one or all open files |
feof | Test for end-of-file |
ferror | Information about file I/O errors |
fgetl | Read line from file, removing newline characters |
fgets | Read line from file, keeping newline characters |
fopen | Open file, or obtain information about open files |
fprintf | Write data to text file |
fread | Read data from binary file |
frewind | Move file position indicator to beginning of open file |
fscanf | Read data from text file |
fseek | Move to specified position in file |
ftell | Position in open file |
fwrite | Write data to binary file |
导入文本数据文件与低级别的I/O
MATLAB低层次的导入文本数据文件提供了以下函数:
fscanf函数读取文本或ASCII文件格式的数据。
fgetl和fgets的函数读取一行的文件,换行符分隔每一行。
fread函数读出的数据流的字节或位的级别。
例子
我们有myfile.txt 文本数据文件保存在我们的工作目录。该文件存储降雨量数据为3个月,6月,7月和2012年8月。
myfile.txt 包含重复的数据集的时间,一个月的雨量测量五个数据项。头数据存储数个月的中号,所以我们有M组测量。
该文件看起来像这样:
Rainfall Data
Months: June, July, August
M=3
12:00:00
June-2012
17.21 28.52 39.78 16.55 23.67
19.15 0.35 17.57 NaN 12.01
17.92 28.49 17.40 17.06 11.09
9.59 9.33 NaN 0.31 0.23
10.46 13.17 NaN 14.89 19.33
20.97 19.50 17.65 14.45 14.00
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 NaN 24.89 19.33
30.97 49.50 47.65 24.45 34.00
18.23 30.34 27.95 16.46 19.34
30.46 33.17 NaN 34.89 29.33
30.97 49.50 47.65 24.45 34.00
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
NaN 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
NaN 21.19 25.85 25.05 27.21
26.79 24.98 12.23 16.99 18.67
我们将数据导入此文件,并显示这些数据。采取以下步骤:
用fopen函数打开的文件并获得文件标识符。
描述文件中的数据格式说明符,如'%s'为一个字符串,'%d'为一个整数,或“%f'表示一个浮点数。
要跳过文字字符的文件,包括他们的格式描述。要跳过一个数据字段,在符使用一个星号(“*”)。
例如,要读取头,并返回单个的M值,我们这样写:
M = fscanf(fid, '%*s %*s %*s %*s %*s %*s M=%d ', 1);
缺省情况下,fscanf读取数据,直到它可以根据我们的格式说明描述的数据不匹配,或它到达文件末尾的。在这里,我们将使用for循环阅读3组数据,每一次,它会读取7行5列。
我们将创建一个名为mydata 在工作区中,从文件中读取数据存储结构。这种结构具有三个字段 - 时间,月和raindata阵列。
创建一个脚本文件,并键入下面的代码:
filename = '/data/myfile.txt';
rows = 7;
cols = 5;
% open the file
fid = fopen(filename);
% read the file headers, find M (number of months)
M = fscanf(fid, '%*s %*s
%*s %*s %*s %*s
M=%d
', 1);
% read each set of measurements
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,
% so transpose the results
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);
当您运行该文件,它会显示以下结果:
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