MATLAB Answers

How do I use the ismember function (if possible) to locate the min/max GMT time elements within a cell array?

3 views (last 30 days)
Brad
Brad on 4 Feb 2016
Commented: Brad on 6 Feb 2016
I have the following 5x4 cell array;
z = { '21:48:50.53' 78530.5390000000 2 580;
'21:48:51.28' 78531.2890000000 4 581;
'21:48:52.03' 78532.0390000000 0 4;
'21:48:52.80' 78532.8050000000 0 4;
'21:48:53.55' 78533.5550000000 1 4};
I am attempting to create a subset of data (A) based on the values in column 4 using the following;
A = z(ismember(z(:, 4), [4]), :);
with the intent of attaining the minimum and maximum values in column 1 (GMT Time).
So using the above cell array, the minimum (earliest) time would be 21:48:52.03 and the maximum (latest) time would be 21:48:53.55.
However, when I attempt to create the subset of data, I get the following error message;
??? Error using ==> cell.ismember at 28 Input must be cell arrays of strings.
Error in ==> GMT_Time_Extraction at 10 A = a(ismember(a(:, 4), [4]), :);
I suspect the problem rests in the GMT times in column 1 or the way I’m attempting to use the ismember function. But in reading the documentation I’m not sure what I’m missing.
Any suggestions are greatly appreciated.
Thank you.

  0 Comments

Sign in to comment.

Accepted Answer

David Young
David Young on 4 Feb 2016
Edited: David Young on 4 Feb 2016
The problem is that you are passing ismember a cell array containing numbers, but it can handle cell arrays only if all they contain is strings. You can easily convert the cell array of numbers to an ordinary numerical array, for example with
A = z(ismember([z{:, 4}], [4]), :);
which uses {} to extract all the elements of column 4 from their cells, then an outer [] to concatenate those into a numerical vector.
Alternatively, you can do the conversion with an explicit call:
A = z(ismember(cell2mat(z(:, 4)), [4]), :);
Note the difference between z{...} in the first method and z(...) in this one.
Since you only want to see whether the value is equal to 4, ismember is overkill. You can more simply and readably use
A = z([z{:, 4}] == 4, :);
or if you prefer
A = z(cell2mat(z(:, 4)) == 4, :);
Finding the maximum and minimum times in column 1 is a separate problem, but easily tackled using datenum.

  1 Comment

Brad
Brad on 6 Feb 2016
David, thanks for taking a look at this. Problem solved and the min/max values in column 1 have been ID'ed.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!