Live Editor: Brace indexing changes Table to Struct

조회 수: 4 (최근 30일)
Kevin Laabs
Kevin Laabs 2022년 12월 15일
댓글: Voss 2022년 12월 15일
Hi guys,
I have a table (basicly imported from multiple .csv and combined them in .-annotation) with some doubles and some strings. Everything worked until I expanded on my code to do some analysis on the doubles of the table.
As soon as I use the brace indexing for the table (for example to get all the zeros in my rigth hand double data) or if I try to get the height of the table, the live editor breaks the table, converts it to a struct and refuses the {}-brace indexing. If I terminate my code before the indexing and use the exact same lines of codes that would follow in the live script (basically copy paste per line) in the command window, everything works as expected. The table stays as a table and the information can be processed with brace indexing.
So I have a table A with ~500 rows and 11 columns. column 1 and 4 are strings the rest are doubles.
If I use:
number=sum(A.all{i,5:end},1)
I get:
Brace indexing is not supported for variables of this type.
If I use it in the Command window (without executing it in the live Editor), it works as intended and the Table is untouched. If I try the line in the Command window after the line gets used in the live editor I get the same error message as in the live editor and I found that the table gets converted to a struct with 11 fields.
The conversion of the table to a struct also happens with:
test1=table2array(A.all(:,5:end))
Error using table2array (line 34)
Input argument must be a table.
And:
lengthofTable=height(A.all)
Which results in a wrong result of test1=1 (instead of the expected ~500)
This happens in 2022b and 2021b...
Since I can't post most of the code and especially not the .csv files that generate the table in the first place, I am unsure how to troubleshoot this. Please guide me in what I can provide and how to troubleshoot this here as best as possible.
Thanks in advance,
Kevin
  댓글 수: 3
Kevin Laabs
Kevin Laabs 2022년 12월 15일
이동: Voss 2022년 12월 15일
Something very weird is happening.
I found that my first column in A.all was al cell vector instead of strings. I fixed that and tried the height-function again.
This time height works (the rest doesn't especially the {}-indexing) but in the live editor it results to 1 while the copy & pasted line in the command window results to the correct ~500 lines. Why is it behaving so differently, I am really confused.
Voss
Voss 2022년 12월 15일
As Peter Perkins said, "You can certainly post something like the table you have."
Can you construct and post a simplified version of your code and data that appears to exhibit the same difference in behavior in the Command Window vs the Live Editor? (Maybe in attempting to do that, you'll discover what the problem is.)

댓글을 달려면 로그인하십시오.

답변 (2개)

Voss
Voss 2022년 12월 15일
Your table is A, but all of the code we can see here involves A.all. Shouldn't you be doing, for instance:
number=sum(A{i,5:end},1)
instead of:
number=sum(A.all{i,5:end},1)
?

Peter Perkins
Peter Perkins 2022년 12월 15일
You can certainly post something like the table you have.
In any case, if A is a table, then A.all is a variable in that table. Maybe a double column vector, maybe something else. A(:,5:end) is the 5th though end'th value of the all variable in A. There's no reason to call table2array on it, because it is not a table.
A.all{i,5:end} tries to do cell indexing on A.all. if A.all is a double, that isn't going to work.
I recommend that you take a look at the table doc.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by