5

I have a file with a data value and timestamp that I am trying to split in Matlab. This is the format:

-18.151346    Mon Jan 28 11:33:08 2013

I am using the textscan function to try and split it.

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

I am trying to split the timestamp into separate columns so that I can just use the time and not the date or year. I had a look at some previous questions which were very similar but for some reason I just can't get it to do what I want. My resulting cell array is in this format.

Column 1     Column 2   Column 3 
-18.151346   Mon       Jan 28 11:33:08 2013

I am completely new to Matlab so any help would be greatly appreciated. Thanks in advance.

slayton
  • 20,123
  • 10
  • 60
  • 89
Bob
  • 53
  • 3

2 Answers2

1
  1. You're using a 'space' string as a delimiter, which is illegal in textscan. Specify it as ' ' instead.
  2. You want treat consecutive spaces as one, so you should also set the 'MultipleDelimsAsOne' flag to 1.

The correct syntax should be:

textscan( fid, '%f%s%s%s%s%n', 'delimiter', ' ', 'MultipleDelimsAsOne', 1);

Had you not tried to tinker with the delimiter option, this behavior would've been done properly by default, so just omit all options:

textscan( fid, '%f%s%s%s%s%n');

Also note that you need a flag for each item in the string that is surrounded by spaces. In other words, for a string like this:

-18.151346 Mon Jan 28 11:33:08 2013

the timestamp in string form will be stored in the 5th column of the resulting cell array.

Eitan T
  • 32,660
  • 14
  • 72
  • 109
slayton
  • 20,123
  • 10
  • 60
  • 89
  • +1: Also, the asterisk can be used to ignore columns, so using `'%*f %*s %*s %*s %*s %*n'` as a format string allows you to extract only the time. – Eitan T Jan 28 '13 at 15:13
  • Using the format you provided (and various other combinations) leaves me with the same output as before. Using the asterisk to try and isolate the time just leaves me with a blank data table. My second column reads 'Mon J' so its as if its not recognizing the space as the delimiter? – Bob Jan 28 '13 at 15:33
  • @Bob you specified the delimiter incorrectly, please refer to the revised answer. Also, forgive me for putting an extra asterisk in the format string, use this instead: `'%*f %*s %*s %*s %s %*n'`. I have tested it and it works now. – Eitan T Jan 28 '13 at 16:08
  • Yes! Omitting all the options works perfectly, Thank you very much. – Bob Jan 28 '13 at 17:43
0

To read the timestamp I'd use regexp.

The solution I'm gonna advise can be optimized, but it can help you at least as first try.

A = regexp(str,'[^\s]+','match');

This way you match all the regular expressions within your string. Then, you know that values and time are stored as element 1 and 5.

I made a short test with:

cell = {'-18.151346 Mon Jan 28 11:33:08 2013','19.33333 Tue Feb 29 10:12:23 2012'};

and this leads you to a cell A{1,2}.

Again, this procedure is perfectible, but you can use it as hint.

fpe
  • 2,700
  • 2
  • 23
  • 47
  • This might work. I need to plot the value (-18.151346) against the time (11:33:08) so is there a way of extracting both at once. Also, that one column is part of a results file with 1440 entries so what would be the easiest way to process all of them at once. If I create a cell array with all the data and then covert it to a matrix and call it out one line at a time would that work? Or is there an easier way? – Bob Jan 28 '13 at 15:42
  • do the values have a standard number of digits? – fpe Jan 28 '13 at 15:52
  • I'm using the first solution as it fits better with what I am trying to do. Thanks for your input though. – Bob Jan 28 '13 at 17:48