MATLAB Answers

Data type conversion help

2 views (last 30 days)
I really need some help directing me to where I can find the answers. MATLAB just saying there is an issue when it runs doesn't help.
I am trying to build a report row selection tool that has all the possible row text that matches the numeric data. I have a corresponding row "table or some data type MATLAB doesn't get" with text and a column with 1/0 to tell what to report. If a 1, report, if 0 then skip that row. The associated data "structure" has all the rows and over many years. It is not a timetable, just data to report based on the users selection setting the 1/0 to what they want to see. There are 155 total rows in these tables.
I'm seeing that data type conversion DOESN'T work, and I need to understand why so I don't have this issue in the future. And I haven't found anything in the help or Community or searching to answer why.
So here is my issue. I'm setting the "indices" to report via the find on the selection "table or structure or whatever it is".
>> indices = find(Rpt_row_sel(:, 2)==0);
MATLAB says "Undefined operator '==' for input arguments of type 'table'."
Why doesn't it work with tables? I have text in column 1 and a number in column 2. If column 2 is a 0, then that row doens't get reported.
So I converted it to all cells in y.
>> y = table2cell(Rpt_row_sel);
>> indices = find(y(:, 2)==0);
Undefined operator '==' for input arguments of type 'cell'.
It still doesn't work. What is the issue with MATLAB not doing what it should do? There is no confusion. If there is, then MATLAB should tell what the confusion is.
Thanks for educating me as I'm just not getting this data type issue stuff.

Accepted Answer

Walter Roberson
Walter Roberson on 16 Jan 2020
When you use () indexing with a table you get back a table. You need {} indexing rpt_row_sel{:, 2}

More Answers (2)

meghannmarie on 16 Jan 2020
Edited: meghannmarie on 16 Jan 2020
A cell or table is a wrapper for the data. You can't compare the cells or tables, you want to compare data inside the cell or table. So you want to pull the data out of its "wrapper". You do that by using curly brackets instead of parentheses. When you pull data out of a table or cell, you need to concatenate it together because it gives you each entry separately, so encapsulate that in square brackets.So try this instead:
indices = find([Rpt_row_sel{:, 2}]==0);
y = table2cell(Rpt_row_sel);
indices = find([y{:, 2}]==0);

Spencer Chen
Spencer Chen on 16 Jan 2020
First of all, assuming that your Rpt_row_sel is a table, then Rpt_row_sel(:,2) will also output a table, which cannot be compared against a scalar. You would need to "dot" the field name or use "{ }" curly brackets to extract the contents of the table column in an arrya of its native format:
indices = find(Rpt_row_sel{:, 2}==0);
This should work providing that the native format for our column are scalars.
If you have store them as cells otherwise, you would need a further step of converting cells into a numeric array for your "==0" comparison to work.





Community Treasure Hunt

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

Start Hunting!