How to extract multi-format data from file?

Hello, I want to extract information from two IMUs, from a textfile that looks like this:
A,70,00000000,0000000c,fffffefa;M,70,ffffff62,ffffff92,fffffda3;G,70,00000004,0000006e,00000001,ffffc4b0; A,71,fffffffa,fffffff9,fffffef1;M,71,0000002a,0000000f,fffffc77;G,71,0000001c,0000001a,ffffffb9,ffffc2c0;
and so on, where 70 and 71 is the ID of the two sensors and A, G, M is accelerometer, gyro and magnetometer, respectively. So I have a char, an int and 3 or 4 hex values (or, if you like, just a long string), separated by commas and semicolons..
In any way, how can I retrieve this and put this into a matrix or similar?

回答(3 个)

textscan with a format of '%c,%f,%[01234567899abcdef],%[0123456789abcdef],%[0123456789abcdef];' repeated 3 times per line, and convert the hex text to numeric values afterwards
OR
fscanf() with %x in place of those %[01234567899abcdef]
Thank you for the response, however, this doesn't work since the file isn't completely formatted, but alters between the following formats:
'%c,%f,%x,%x,%x;' %(acc, mag)
'%c,%f,%x,%x,%x,%x;' %(gyro incl temperature)
so fscanf stops when it reaches any inconsistency. Maybe textscan can handle this issue better?
Right now I use format '%c' and step throuh it to see whether it is A,M or G, and then store in 3 or 4 hex values by applying strcat on every 8 hex character chunk. This seems rather inefficient, though.
Thanks again
Walter meant:
fid = fopen('C:\Users\Oleg\Desktop\test.txt');
data = fscanf(fid,'%c,%f,%x,%x,%x;%c,%f,%x,%x,%x;%c,%f,%x,%x,%x,%x;\n');
fid = fclose(fid);
data = reshape(data,16,[]).';
% A, M and G are stored in numeric format with ascii correspondence
char(data(:,1))

提问:

2011-4-4

Community Treasure Hunt

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

Start Hunting!

Translated by