开发者

How can I read this file in MATLAB?

I have a file named data.dat with the following contents:

my name is elyas 123

this is a book 123.450

my father name -2.34e+05

I want开发者_JS百科 load this file into MATLAB and get the following data as output:

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

But I don't know how to do this. Any suggestions?


Here's one way to do it using TEXTSCAN to read each of the 3 lines:

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


You could try textscan.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜