Error when using logical indexing to get a value from a table
22 views (last 30 days)
Show older comments
Robert Demyanovich
ungefär 21 timmar ago
I have the following snippet of code, which I created based on online tutorial:
theta = pi/4
%Create the table
Angles = {pi/2;pi/3;pi/4;pi/6;pi/12};
wValues = {0;0.8846;1.318;1.973;3.782};
%Create a table of w values at specific angles
T = table(Angles,wValues);
%Read the value of w from the table
logical_index = T.Angles == theta;
w = T.wValues(logical_index);
I keep getting the following error on the line:
logical_index = T.Angles == theta;
Operator '==' is not supported for operands of type 'cell'.
I don't understand why this is happening since I am following a code example.
0 Comments
Accepted Answer
Stephen23
ungefär 4 timmar ago
Edited: Stephen23
ungefär 2 timmar ago
"Operator '==' is not supported for operands of type 'cell'."
Why are you using a cell array to store scalar numeric data?
"I don't understand why this is happening since I am following a code example."
Cell arrays are not defined for logical operations like EQ. Use numeric vectors instead:
theta = pi/3;
Angles = [pi/2;pi/3;pi/4;pi/6;pi/12]; % numeric vector, NOT a cell array!
wValues = [0;0.8846;1.318;1.973;3.782]; % numeric vector, NOT a cell array!
T = table(Angles,wValues);
logical_index = T.Angles == theta;
w = T.wValues(logical_index)
More Answers (1)
Walter Roberson
ungefär 3 timmar ago
Examine the table that gets created:
%Create the table
Angles = {pi/2;pi/3;pi/4;pi/6;pi/12};
wValues = {0;0.8846;1.318;1.973;3.782};
%Create a table of w values at specific angles
T = table(Angles,wValues)
Notice that it is a 5 x 2 table, with the entries being cell arrays. Those cell array entries cannot be compared to numeric values.
There is a connection between cell arrays and tables, but it has to do with appending data to tables.
T1 = table((1:5).', (101:105).')
data_to_add = [Angles,wValues]
T1(end+1:end+5,:) = data_to_add
So, an existing table can be extended by assigning a cell array.
Or you can
T2 = cell2table(data_to_add)
See Also
Categories
Find more on Matrix Indexing 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!