-1

我有一个名为data.dat以下内​​容的文件:

我的名字是 elyas 123

这是一本书 123.450

我父亲的名字-2.34e+05

我想将此文件加载到 MATLAB 中并获取以下数据作为输出:

a = 123
b = 123.450
c = -2.34e+05
name = 'elyas'

但我不知道该怎么做。有什么建议么?

4

2 回答 2

4

这是使用TEXTSCAN读取 3 行中的每一行的一种方法:

fid = fopen('data.dat','rt');                 %# Open the file
data = textscan(fid,'%*s %*s %*s %s %f',1);   %# Read the first line, ignoring
                                              %#   the first 3 strings
name = data{1}{1};                            %# Get the string 'name'
a = data{2};                                  %# Get the value for 'a'
data = textscan(fid,'%*s %*s %*s %*s %f',1);  %# Read the second line, ignoring
                                              %#   the first 4 strings
b = data{1};                                  %# Get the value for 'b'
data = textscan(fid,'%*s %*s %*s %f',1);      %# Read the third line, ignoring
                                              %#   the first 3 strings
c = data{1};                                  %# Get the value for 'c'
fclose(fid);                                  %# Close the file
于 2010-06-21T18:02:33.460 回答
1

你可以试试textscan

于 2010-06-21T17:40:38.747 回答