Table Indexing and trained model prediction

3 次查看(过去 30 天)
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 个评论
Guillaume
Guillaume 2018-2-20
编辑:Guillaume 2018-2-20
@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.
Suraj Mankulangara
Suraj Mankulangara 2018-2-20
@Guillaume
Thanks for the heads up! :) I thought I HAD posted an answer instead of a comment!

请先登录,再进行评论。

回答(1 个)

Suraj Mankulangara
Suraj Mankulangara 2018-2-20
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

类别

Help CenterFile Exchange 中查找有关 Support Vector Machine Regression 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by