Table Indexing and trained model prediction

조회 수: 1 (최근 30일)
Sally Britnell
Sally Britnell 2018년 2월 4일
댓글: Suraj Mankulangara 2018년 2월 20일
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에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by