Table Indexing and trained model prediction

Last year I wrote some code which worked well with 2017a. I ran this with 2017b it gives the following error.
Error using test (line 14) You cannot subscript a table using linear indexing (one subscript) or multidimensional indexing (three or more subscripts). Use a row subscript and a variable subscript.
height = 107;
gender = "M";
ethnicity = 2;
age = 7;
weightModel = AEGH;
table = table(age,ethnicity,gender,height);
weight = weightModel.predictFcn(table);
disp(weight);
My only thought is a change between versions. However, no one else seems to be reporting this occurring so perhaps I have done something odd.
Help appreciated.

5 commentaires

Hi Sally,
It looks like the issue is with the following line:
table = table(age,ethnicity,gender,height);
This issue occurs since the variable name 'table' on the left-hand side is the same as the built-in MATLAB function 'table' on the right-hand side.
The first time this code is run, it works fine. The function 'table()' is invoked, and the result is stored in the variable 'table'.
If the workspace is not cleared after the execution of this code, subsequent invocations of the 'table()' function will be treated as indexing. This occurs since the variable 'table' still exists in the workspace. MATLAB gives variables higher precedence over functions of the same name.
In order to resolve this issue, you could try giving a different name to the result variable. For example:
resultTable = table(age, ethnicity, gender, height);
Sincerely,
Suraj Mankulangara
KSSV
KSSV le 20 Fév 2018
What is AEGH?
Sally Britnell
Sally Britnell le 20 Fév 2018
AEGH is the name of the trained linear regression model.
I will also check the table variable next to one I am at my computer.
Thanks for the suggestion I never noticed that - doh
Guillaume
Guillaume le 20 Fév 2018
Modifié(e) : Guillaume le 20 Fév 2018
@Suraj,
Since you identify the problem correctly, you should move your comment to an answer, so you can get reputation points for it.
@Sally,
Yes, do not use table as a variable name since it then prevents you from using the table function. Other function names that are commonly shadowed by mistakes are: sum, mean, cell, struct, image and even i and j although the latter two are rarely a problem.
@Guillaume
Thanks for the heads up! :) I thought I HAD posted an answer instead of a comment!

Connectez-vous pour commenter.

Réponses (1)

Hi Sally,
It looks like the issue is with the following line:
table = table(age,ethnicity,gender,height);
This issue occurs since the variable name 'table' on the left-hand side is the same as the built-in MATLAB function 'table' on the right-hand side.
The first time this code is run, it works fine. The function 'table()' is invoked, and the result is stored in the variable 'table'.
If the workspace is not cleared after the execution of this code, subsequent invocations of the 'table()' function will be treated as indexing. This occurs since the variable 'table' still exists in the workspace. MATLAB gives variables higher precedence over functions of the same name.
In order to resolve this issue, you could try giving a different name to the result variable. For example:
resultTable = table(age, ethnicity, gender, height);
Sincerely,
Suraj Mankulangara

Catégories

En savoir plus sur Deep Learning Toolbox dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by