Question

I have a text file full of sensor data which has the following format per line:

Date: 2014-04-22_09-48-16.547400 Timestamp:  2873652.086362542 Gyro.X: -0.0372661240398884 Gyro.Y:  0.0541165024042130 Gyro.Z:  0.1216094791889191 UserAccel.X: -0.0201703608036041 UserAccel.Y: -0.0118222190067172 UserAccel.Z: -0.0187157746404409 Gravity.X: -0.9937761425971985 Gravity.Y: -0.0025820778682828 Gravity.Z: -0.1113654002547264

All I want from the file are the numeric values for timestamp, gyro, acceleration, and gravity.

I've tried using space as a delimiter but I'm running into problems since there are two spaces preceding each value if the value is positive, but only one if negative.

So far I have the following, but it's only going to work if there's one space between each value (each value would have to be negative).

data = textscan(fid, '%*s %*s %*s %*s %f %*s %f %*s %f %*s %f %*s %f %*s %f %*s %f %*s %f %*s %f %*s %f', 'delimiter', ' ');

Is there a way around this?

Was it helpful?

Solution

Set the option 'MultipleDelimsAsOne' to 1 like this:

string = 'Date: 2014-04-22_09-48-16.547400 Timestamp:  2873652.086362542 Gyro.X: -0.0372661240398884 Gyro.Y:  0.0541165024042130 Gyro.Z:  0.1216094791889191 UserAccel.X: -0.0201703608036041 UserAccel.Y: -0.0118222190067172 UserAccel.Z: -0.0187157746404409 Gravity.X: -0.9937761425971985 Gravity.Y: -0.0025820778682828 Gravity.Z: -0.1113654002547264';
data = textscan(string,...
    '%*s %*s %*s %*s %*s %f %*s %f %*s %f %*s %f %*s %f %*s %f %*s %f %*s %f %*s %f',...
    'delimiter', ' ', 'MultipleDelimsAsOne', 1);

At the start of the format string there was one %*s missing and there was one unnecessary %*s %f at the end of the format string that I have removed. This yields a cell containing 9 floating point values.

OTHER TIPS

You may use importdata that uses just a single space as delimiter to separate out elements into different cells of a cell array.

Code

imp_data = importdata(inputtext_filename,' ')

t_data = imp_data.textdata
d_data = cellfun(@str2num,t_data,'uni',0)

data1 = [cell2mat(d_data) imp_data.data]

Date1 = cell2mat(t_data(:,2))
Timestamp = data1(:,1)

Gyro = struct('X', num2cell(data1(:,2)), 'Y', num2cell(data1(:,3)), 'Z', num2cell(data1(:,4)))
UserAccel = struct('X', num2cell(data1(:,5)), 'Y', num2cell(data1(:,6)), 'Z', num2cell(data1(:,7)))
Gravity = struct('X', num2cell(data1(:,8)), 'Y', num2cell(data1(:,9)), 'Z', num2cell(data1(:,10)))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top