Error when using logical indexing to get a value from a table

30 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

Stephen23
Stephen23 am 14 Feb. 2026 um 18:18
Bearbeitet: Stephen23 am 14 Feb. 2026 um 20:19
"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)
w = 0.8846
  3 Kommentare
Steven Lord
Steven Lord vor etwa 3 Stunden
A cell array is a data type that can store different types and sizes together in one array. This flexibility (you can store anything in it) comes with some cost (you can store anything in it, so certain operations don't make sense to perform on it and aren't defined on it, and some operations have extra overhead as they check if the data in the array has certain characteristics the operation requires the data to have.)
For example, want to check the length of pieces of text in an array? If you use the strlength function on a string array, MATLAB knows (from its class) that it does contain text data rather than (say) numeric data.
S = "abracadabra";
strlength(S)
ans = 11
But if you pass in a cell array, it might work or it might not, depending on whether the cell array contains text data. MATLAB has to check and throw an error if the cell doesn't have text data. Imagine you had a large cell array where only the last element was not text data; MATLAB would have to look through the whole thing before deciding "The question doesn't make sense, I'll throw an error."
C = {'abracadabra'};
strlength(C) % works
ans = 11
D = {42};
strlength(D) % errors
Error using strlength
First argument must be text.
Trying to figure out what == equality means for a cell (which again could contain just about anything) and another piece of data would have so many edge/corner cases that I believe trying to explain it would just confuse people even more. So == isn't defined for cell arrays.
Robert Demyanovich
Robert Demyanovich vor etwa 5 Stunden
Thank you for that very informative yet succinct explanation of cell arrays.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson vor etwa 5 Stunden
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)
T = 5×2 table
Angles wValues __________ __________ {[1.5708]} {[ 0]} {[1.0472]} {[0.8846]} {[0.7854]} {[1.3180]} {[0.5236]} {[1.9730]} {[0.2618]} {[3.7820]}
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).')
T1 = 5×2 table
Var1 Var2 ____ ____ 1 101 2 102 3 103 4 104 5 105
data_to_add = [Angles,wValues]
data_to_add = 5×2 cell array
{[1.5708]} {[ 0]} {[1.0472]} {[0.8846]} {[0.7854]} {[1.3180]} {[0.5236]} {[1.9730]} {[0.2618]} {[3.7820]}
T1(end+1:end+5,:) = data_to_add
T1 = 10×2 table
Var1 Var2 ______ ______ 1 101 2 102 3 103 4 104 5 105 1.5708 0 1.0472 0.8846 0.7854 1.318 0.5236 1.973 0.2618 3.782
So, an existing table can be extended by assigning a cell array.
Or you can
T2 = cell2table(data_to_add)
T2 = 5×2 table
data_to_add1 data_to_add2 ____________ ____________ 1.5708 0 1.0472 0.8846 0.7854 1.318 0.5236 1.973 0.2618 3.782
  1 Kommentar
Robert Demyanovich
Robert Demyanovich vor etwa 4 Stunden
When I look at Table "T" in the workspace I do not see any brackets whatsoever around the data. To me it looked like a typical table with data values.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by