File 如何在MATLAB中读取此文件?
我有一个名为File 如何在MATLAB中读取此文件?,file,matlab,matlab-load,File,Matlab,Matlab Load,我有一个名为data.dat的文件,其中包含以下内容: 我叫elyas 123 这是一本123.450的书 我父亲的名字-2.34e+05 我想将此文件加载到MATLAB中,并获得以下数据作为输出: a = 123 b = 123.450 c = -2.34e+05 name = 'elyas' 但我不知道怎么做。有什么建议吗?你可以试试。这里有一种方法,可以使用阅读这三行中的每一行: fid = fopen('data.dat','rt'); %# Open
data.dat
的文件,其中包含以下内容:
我叫elyas 123
这是一本123.450的书
我父亲的名字-2.34e+05
我想将此文件加载到MATLAB中,并获得以下数据作为输出:
a = 123
b = 123.450
c = -2.34e+05
name = 'elyas'
但我不知道怎么做。有什么建议吗?你可以试试。这里有一种方法,可以使用阅读这三行中的每一行:
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
我认为你需要给出格式规则。那么,名字总是在“我的名字是”之后的第一个标记吗?在那之后,a永远是最后的标志吗?b呢,它是第三行的最后一个标记,第一个标记之后的第一个标记,以字母b开头?那么c呢,它是最后的代币吗?要解析这个文件,我们需要知道文件格式的规则。