Trying to make a 2 value sz vector, get Error using tabular/horzcat All input arguments must be tables.

6 views (last 30 days)
Seems so simple, I'm trying to set a new table size based on values in a previous table
C = sum(app.UITable2.Data(1,:));
No matter how hard I try I can't get the sum of a single column!
So I give up and do
sz = [C(1,1) 4];
Get the weird error that all inputs must be tables!
Try making sz a known vector type
make a property
sz = [4 4]
then try
app.sz = [C(1,1) 4];
Sorry for all the asks but I'm a very experienced C, C++ (and many other languages) programmer but this .m code is driving me crazy! Help give all kinds of useless details about setting Fonts etc but little about simple syntax for e.g. summing a column.
S = sum(A,1); does sum by columns (vs ,2 for by rows but no way to say just do column 1. Even though C is a 1x4 Can't seem to get the scaler C(1,1) out of it properly.
C(1,1) or C(1) works in the command line, just not in my app created by app designer.
Ultimately all I really want is to make a new table of the correct size with
app.TrialsTable = table('Size',sz,'VariableTypes',{'uint8','uint8','categorical','int16'});

Accepted Answer

Voss
Voss on 6 Jul 2024
T = table([1;2;3;4],{'ok';'let''s';'see';'here'})
T = 4x2 table
Var1 Var2 ____ _________ 1 {'ok' } 2 {'let's'} 3 {'see' } 4 {'here' }
Given the table T above, T(1,:) is another table containing the first row of T:
T(1,:) % returns a table
ans = 1x2 table
Var1 Var2 ____ ______ 1 {'ok'}
and T(:,1) is another table containing the first column of T:
T(:,1) % returns a table
ans = 4x1 table
Var1 ____ 1 2 3 4
In other words, subscripting a table using parentheses returns another table. To get the data out of a table, you can use curly brace subscripting. For example, to get the first column of table data:
T{:,1} % returns a numeric column vector, not a table
ans = 4x1
1 2 3 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
sum(T{:,1})
ans = 10
sz = [sum(T{:,1}) 4]
sz = 1x2
10 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
The "weird error that all inputs must be tables!" is due to trying to horizontally concatenate a table with a non-table, as in
[T(1,1) 4]
Error using tabular/horzcat (line 223)
All input arguments must be tables.
because T(1,1) is a table and 4 is not.
  6 Comments
Voss
Voss on 7 Jul 2024
Any of the following:
app.TrialsTable.Var3{ii+xx} = 'R';
app.TrialsTable.Var3(ii+xx) = {'R'};
app.TrialsTable{ii+xx, 3} = {'R'};
Alessandro Livi
Alessandro Livi on 10 Jul 2024
What works is this:
LRCat = categorical({'L';'R'}); % Categorical (array?)
app.TrialsTable.Var3(ii+xx) = LRCat(1); % set left

Sign in to comment.

More Answers (0)

Categories

Find more on Categorical Arrays in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!