tags:

views:

59

answers:

2

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 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?

+1  A: 

You could try textscan.

Jouni K. Seppänen
+4  A: 

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
gnovice

related questions