0
votes

I have a cell array in MATLAB that has the following columns..

Timestamp  Info      Joint          X        Y          Z     State
0.4449    'BASE05'    'SpineBase' -0.4222   -0.5245    2.681 'Tracked' 

The 0.4449 needs to be converted to a timestamp format which I have so far been achieving by extracting column 1 and performing datestr on.

time = num(:,1);
time = num2cell(time);
Bodytime = datestr(cell2mat(time), 'HH:MM:SS');

This gives me a character array with all the timestamps.

However, I need to put this character array back into the first column of cell array. I'm having some trouble here, I was trying to convert my cell array to character array but as I have a mix of numbers and string I don't think that's the way forward. I also tried to replace the first column from the bodytime character array into my original cell array BodyData, but I don't think that's the way forward either.

Ideally I need to get something like this

Timestamp  Info      Joint          X        Y          Z     State
10:44:59   'BASE05'    'SpineBase' -0.4222   -0.5245    2.681 'Tracked' 

My main goal here is to look up a certain timestamp of when an event happened and extract/plot (tbd) all the relative information for that time.

1
can't you just cat them together? I mean you can concatenate the column back to the front by doing something like [new_column,old_cell_array]; - GameOfThrows

1 Answers

2
votes

When you call datestr with an array of timestamps, what you are actually getting back is a "matrix" of characters, where each row is in the format HH:MM:SS. There is no greater separation between the rows of this matrix than there is between the columns of it (where column 3, for example, is the first : character of each timestamp).

You can certainly split this array back into a cell array, where each cell element is a single timestamp, which can then go back into your source cell array. To do this use mat2cell:

BodytimeCell = mat2cell(Bodytime,size(time));

However there is probably unnecessary complexity in what you've already done. Most simply, if your aim is a cell array of timestamp strings, you could do this directly with cellfun:

BodytimeCell = cellfun(@(t) datestr(t, 'HH:MM:SS'), time, 'UniformOutput',false);

However you might also consider if it really helps to store the timestamps as a strings at all. It may be more advantageous to store them as a datetime array, which can remember the format you want to display it in while still allowing you to work with the value numerically if you need to.

The structure of the data as a whole also suggests it may be better to store as a table than a cell array. This gives you more natural ways to interact with the data - for example, the numeric columns in a table can be treated like numeric arrays directly, rather than having to convert from a cell array of numbers.