importing data with multiple delimiters

1 view (last 30 days)
Here is an example of my dataset...
-1 1:6.000000 2:148.000000 3:72.000000 4:35.000000 5:0.000000 6:33.599998 7:0.627000 8:50.000000
+1 1:1.000000 2:85.000000 3:66.000000 4:29.000000 5:0.000000 6:26.600000 7:0.351000 8:31.000000
-1 1:8.000000 2:183.000000 3:64.000000 4:0.000000 5:0.000000 6:23.299999 7:0.672000 8:32.000000
+1 1:1.000000 2:89.000000 3:66.000000 4:23.000000 5:94.000000 6:28.100000 7:0.167000 8:21.000000
The current delimiter is whitespace. While that is correct, I do not want to import the semicolons or the numbers before the semicolons. Is there a simple way to do this?

Accepted Answer

Walter Roberson
Walter Roberson on 17 Nov 2013
fmt = ['%f' repmat('%*f:%f', 1, 8)];
textscan(fid, fmt, 'CollectInput', 1)
That will give a cell with one entry with the one entry being a numeric array, with the first column being the +1 and -1, and the other entries being the floating point numbers.
  2 Comments
Michael
Michael on 17 Nov 2013
Thanks Walter, this lead me in the right direction. I just had to tweak your code slightly. The following worked perfectly.
textscan(fid, fmt, 'CollectOutput', 1)
Walter Roberson
Walter Roberson on 17 Nov 2013
Where is my memory tonight ? :-(

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!