Parsing ascii seperated file with string and number data

170 views
Skip to first unread message

Lis

unread,
Nov 5, 2009, 12:56:17 PM11/5/09
to freemat
Hi all.
I try to use a simple asci delimited file whose colums are seperated
with ;.
I did it this way:
data=dlmread('data.txt', ';');

The file's content is:
aaa;1.2,3
bbb;1.1,4
ccc;1.1,6
eee;1.4,7

but if I write:
data

freemat only returns:
ans =
0 0
0 0
0 0
0 0

My first question is: How to parse the data the right way.
My second question: Is it possible to return a array, or matrix who
holds String and Double data in each row.

Thanks for the advance Louis

Timothy Cyders

unread,
Nov 18, 2009, 2:46:48 PM11/18/09
to fre...@googlegroups.com
Louis,

I don't think dlmread or csvread support reading string data (I may be wrong here, but I've never found a way to make that work), which is why you're getting 0's as output. For example, if we modify your data file so its content is:

1.2,3
1.1,4
1.1,6
1.4,7

Then run

data=dlmread('test.data', ',')

We get

data =
    1.2000    3.0000
    1.1000    4.0000
    1.1000    6.0000
    1.4000    7.0000

I've worked around this using fopen, fgetl and str2num. I've uploaded a script gpst.m I wrote to decode raw GPS data using this exact technique. The skinny is, you can store the whole line as a string using fgetl, then parse it based on known patterns, because in the string x = 'abced', for example, x(2) is b. You can use str2num to convert your numbers into workable array parts, and go from there. This isn't necessarily best, but it's a quick and dirty solution I've used.

There are a couple of ways to store both string data and numerical data, by using cells or data structures. Depending on your application, you might use one or the other (or a combination). The problem here is that data stored in a cell first has to be extracted and converted to a numerical matrix before you can do matrix operations on it. With a structure, you can call structure elements that are matrices/numbers and they will behave as expected. Example:

Using cells:
-->x = {'aaa' 1.2 3;'bbb' 1.1 4; 'ccc' 1.1 6; 'ddd' 1.4 7}

x =
 [aaa] [1.2] [3]
 [bbb] [1.1] [4]
 [ccc] [1.1] [6]
 [ddd] [1.4] [7]

--> x(:,2)

ans =
 [1.2]
 [1.1]
 [1.1]
 [1.4]

ans is still a cell, so we can't directly do matrix operations on it:

--> 3*x(2,2)
Error: Cannot perform type conversions with this type

Using structures:
--> x.text = {'aaa'; 'bbb'; 'ccc'; 'ddd'}; x.data = [1.2 3; 1.1 4; 1.1 6; 1.4 7];
--> x.text
ans =
 [aaa]
 [bbb]
 [ccc]
 [ddd]

--> x.data
ans =
    1.2000    3.0000
    1.1000    4.0000
    1.1000    6.0000
    1.4000    7.0000

--> 3*x.data
ans =
    3.6000    9.0000
    3.3000   12.0000
    3.3000   18.0000
    4.2000   21.0000

Hope this helps.

Cheers,

TJ


--

You received this message because you are subscribed to the Google Groups "freemat" group.
To post to this group, send email to fre...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/freemat?hl=en.



Reply all
Reply to author
Forward
0 new messages