阅读 .txt。 Matlab 中的数据
Reading .txt. data in Matlab
我有一个很基础的table
Alcohol Tobacco
6.47 4.03
6.13 3.76
6.19 3.77
4.89 3.34
5.63 3.47
4.52 2.92
5.89 3.20
4.79 2.71
5.27 3.53
6.08 4.51
4.02 4.56
我试过使用 textscan 阅读它但是一片空白。
fileID = fopen('TabaccoAlcohol.txt');
C_text = textscan(fileID,'%n',2);
在程序中使用 objects 这样的标题会很好,例如酒精将是所有 11 行数据。我知道 Matlab 可以做到这一点,但我做不到。请帮忙
您可以使用下面给出的代码更改您的代码
fileID = fopen('read.txt');
C_text = textscan(fileID,' %f %f');
fclose(fileID);
使用readtable:
>> t = readtable('data.txt')
t =
Alcohol Tobacco
_______ _______
6.47 4.03
6.13 3.76
6.19 3.77
4.89 3.34
5.63 3.47
4.52 2.92
5.89 3.2
4.79 2.71
5.27 3.53
6.08 4.51
4.02 4.56
>> t.Alcohol
ans =
6.4700
6.1300
6.1900
4.8900
5.6300
4.5200
5.8900
4.7900
5.2700
6.0800
4.0200
我有一个很基础的table
Alcohol Tobacco
6.47 4.03
6.13 3.76
6.19 3.77
4.89 3.34
5.63 3.47
4.52 2.92
5.89 3.20
4.79 2.71
5.27 3.53
6.08 4.51
4.02 4.56
我试过使用 textscan 阅读它但是一片空白。
fileID = fopen('TabaccoAlcohol.txt');
C_text = textscan(fileID,'%n',2);
在程序中使用 objects 这样的标题会很好,例如酒精将是所有 11 行数据。我知道 Matlab 可以做到这一点,但我做不到。请帮忙
您可以使用下面给出的代码更改您的代码
fileID = fopen('read.txt');
C_text = textscan(fileID,' %f %f');
fclose(fileID);
使用readtable:
>> t = readtable('data.txt')
t =
Alcohol Tobacco
_______ _______
6.47 4.03
6.13 3.76
6.19 3.77
4.89 3.34
5.63 3.47
4.52 2.92
5.89 3.2
4.79 2.71
5.27 3.53
6.08 4.51
4.02 4.56
>> t.Alcohol
ans =
6.4700
6.1300
6.1900
4.8900
5.6300
4.5200
5.8900
4.7900
5.2700
6.0800
4.0200