indexing with cells (need specific value in all the rows of a column

51 views (last 30 days)
I have data populating in a cell as such:
I'm trying to extract data from all of the rows in column 6. When I try:
tx_data{:,6}(3,:)
but I get:
Expected one output from a curly brace or dot indexing expression, but there were 5
results.
I'm trying to plot those 5 results for a graph that will be updated per measurement. Obviously it sees the 5 results that I'm looking for, but I can't seem to actually get that data to pass to my function (something like what's below...TBD):
plot(gui_app.TRPApp.UIAxes, tx_data{:,6}(3,:));
The number of rows in this cell array will be dependent on the test being run. I may need to do something similar while separating data marked at 'v' or 'h', but I'm not quite sure if I'll need that or not.
Any help would be much appreciated!

Answers (1)

Gaurav Garg
Gaurav Garg on 28 Dec 2020
Edited: Gaurav Garg on 28 Dec 2020
Hi Scott,
While using tx_data{:,6}(3,:), you were expected to get an error. That's because intermediate brace {} indexing produced a comma-separated list, and to perform indexing, it must have produced a single value.
You can get some more info on this from this documentation.
However, there is one other thing you can do -
% Assuming that c is a cell array of size (4,4)
c{1,2} = [2,2,2,2]
c{2,2} = [2,2,2,2]
c{3,2} = [2,2,2,2]
c{4,2} = [2,2,2,2]
[c{[1:4],2}] % This command helps you convert the second column in the cell array to a single-dimensional array.
plot([1:16],[c{[1:4],2}]) % You can use it as a normal 1-D array and plot the values
In your case specifically, you can manipulate this 1-D array to plot only the required values. (tx_data{:,6}(3,:))
For more information, you can look at the doc here.
  1 Comment
Scott
Scott on 5 Jan 2021
Thanks for your response. I see what you mean, and wanted to follow up with the simplest solution I came up with (perhaps it could help others). Bottom line, using easiest way to do with was to use horzcat (I originally tried vertcat, but horzcat made things easier to deal with). The real issue here was understanding how to deal with the comma separated list that was returned.
temp_measured_data = horzcat(tx_data{:,6});
plot(gui_app.TRPApp.UIAxes, temp_measured_data(3,1), '-*b')
Also, per a response from someone at Mathworks:
it looks like you are trying to use multilevel indexing when accessing a range of cells in a cell array. However, you can only use multilevel indexing on a cell array when you are accessing a single cell. Please see the following documentation page for more information on multilevel indexing on cell arrays
This is not possible because when accessing a range of cells (in your case a column of cells containing row vectors), a "comma-separated list" is returned, which cannot be indexed into like an array. Please refer to the following documentation page for more information on comma-separated lists in MATLAB:
This means that your desired workflow must be achieved in two steps: first you must concatenate the column of cell arrays into a single numerical array, then you must access that array to access the elements you want. I will illustrate this with a small example:
>> c = {'a', [1, 2, 3]; 'b', [4, 5, 6]}
c =
2×2 cell array
{'a'} {1×3 double}
{'b'} {1×3 double}
Suppose we have this 2-by-2 cell array, 'c', and we want to access all of the first elements of the row vectors in the second column, i.e. [1, 4]. Then we must first concatenate these row vector cells into an array like so:
>> a = vertcat(c{:,2})
Now 'a' is a 2-by-3 numerical array, and we can access all the elements in the first column as follows:
>> a(:,1)

Sign in to comment.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!