Reputation: 133
I am trying to import data files from an oscilloscope into Matlab. It seems like the scope manufacturer tried to make this task as difficult as possible. Here is what the file looks like: it has three columns, and the values in the last two columns are numbers that might have a letter as suffix for the units (e.g. "m" = milli, "u" = micro, "n" = nano...), but not always, aas you can see in the example.
Right now I am doing something like
data = textscan(fid,'%d%s%s',ns,'Headerlines',1,'Delimiter',',');
where data{1} is a cell array that I can easily convert in to a vector, but data{2} and data{3} are cell arrays of strings. I would like to strip the last character if it is a letter, convert to a vector, and scale each element according to the unit suffix, if there is one.
Here is an excerpt of my file:
No.,Time,CH1
1,-6,0
2,-5.99999,6m
3,-5.99998,0
4,-5.99997,8m
5,-5.99996,-12m
6,-5.99995,6m
...
600006,50u,-4m
600007,60u,-8m
600008,70u,62m
600009,80u,0
600010,90u,70m
600011,100u,-104m
Upvotes: 3
Views: 311
Reputation: 20319
For the last column, if the values are ALWAYS either 0 or terminate with m
you can use regular expressions to strip off the char.
value = regexprep(input_string, 'm', ''); % replace any m's with nothing
You can wrap this in an anonymous function and apply it to the entire cell array with cellfun
convertFn = @(x) str2double( regexprep( x, 'm', '') );
data_column = cellfun(convertFn, data{3});
If the terminating char can be some other value you'll need to apply the solution for the 2nd column which is below.
Converting the 2nd column is a bit more complicated because you want to alter the value based upon the terminating character.
You'll need to write a simple function that strips the char and returns the scaled value.
function v = convert_str(s)
if numel(str)==1 && strcmp(s, '0')
v = 0;
else
v = str2double(s(1:end-1)); % convert all but last char to number
if strcmp( s(end), 'u') )
v = v * 1e-6;
end
end
end
You can then use this new function in a call to cellfun
to convert the second column:
data_column = cellfun(@convert_str, data{2});
Note: The code above isn't tested but it illustrates what you need to do.
Upvotes: 1