import tab and comma delimited dat file

I'm a newbie and am struggling to import a dat file which is a combination of tab and comma delimited data.
I have attached a sample dat file which is very easy to import in excel but in MATLAB i cannot get it to work.
I am trying to create a structure called mydat which will contain (see dat file, note the tabs and commas)
mydat.param1 = 1.01
mydat.param2 = 1.02
mydat.param3 = 1.03
mydat.curves = [1.01 2.01 3.01 4.01;
1.02 2.02 3.02 4.02;
1.03 2.03 3.03 4.03]
mydat.curves_info = [data1; data2; data3; data4]
need help!
my Dat file
------------
Parameteres
------------
param1 (cm) : \t 1.01
param2 (cc) : \t 1.02
param2 (g) : \t 1.03
------------
Information
------------
info1 :
info2 :
info3 :
, data1 , data2 , data3 , data4 ,
, 1.01 , 2.01 , 3.01 , 4.01 ,
, 1.02 , 2.02 , 3.02 , 4.02 ,
, 1.03 , 2.03 , 3.03 , 4.03 ,

 Accepted Answer

I'm not sure if the formatting is exactly how it appears above, but you can probably iron out the kinks yourself. I did assume that the \t are actually tabs. Other than that, I copy-n-pasted what you have posted, and this works:
fid = fopen('foo.dat','rt');
x = textscan(fid,'%*[^:]:\t%f',3,'headerlines',3);
params = x{1}
x = textscan(fid,' , %s , %s , %s , %s , ',1,'headerlines',7);
curves_info = [x{:}]
x = textscan(fid,' , %f , %f , %f , %f , ')
curves = [x{:}]
fclose(fid);
(You can package the data however you see fit.) Note the use of literal text in the textscan format specifier. That's the magic. Also, in the first textscan command, it says "read and ignore everything up to a colon, then there will be a colon and a tab, then read a floating-point number". That's how you get just the numbers from those three lines.

7 Comments

Thanks Matt
Your literal makes me understand the process.
In my data header i have spaces like
, data1 cm , data2 g , data3 F , data4 ,
how do i import with spaces?
Is %8c the best way to do it?
You could do that *if* you know that the field with is fixed. Another way would be
headers = textscan(fid,',%[^,],%[^,],%[^,],%[^,],');
headers = deblank([headers{:}]);
(When you say "with the spaces", I assume you mean the ones inside the strings, like "data2 g", not the leading or trailing ones, like " data2 g ")
Another approach would be to use one instance of fgetl, then use regular expressions to split on the commas.
x = textscan(fid,' %[^,], %[^,] , %[^,] , %[^,] , %[^,] , %[^,] , %[^,] , %[^,], %[^,], ', 'headerlines',16);
mydat.curves = deblank([x{:}]);
this results in an m by n cell
and if i do str2double it gives me what i want but can this be done without using st2double from the textscan itself?
But how can you get numbers out of something like "data2 g"? If you have something numeric, specify that with a numeric format specifier like %f. That's the whole point of using textscan.
The first column is a date and time string (11-16-10 14:57:29 and i dont need this column). Other columns are all numbers so even if i use %f for all other columns except the first one I get a m by n cell
Is there a way i can neglect the first column and use %f for all others?
Use an asterisk in front of any format specifier you want to ignore. Eg '%*f-%*f-%*f %*f:%*f:%*f %f' should read only the numbers after the date/time string.
ok this is good.
learned how to use textscan
this will be of great help
thanks

Sign in to comment.

More Answers (0)

Categories

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!