I want to make a simple table in Matlab, so I used this code:

T := table(a = 13, c = 42)

But it gives an error:

xrdUndefined function 'T' for input arguments of type'char'.Error in xrd (line 1)T := table(a = 13, c = 42)

How can I fix this?

3

Best Answer


:= is no operator in Matlab, this screws up interpreting your line of code. Matlab tries to access a function named T with the rest of the line as string argument.

Try

a = 13b = 42 t = table(a, b)

Output:

t = a b __ __13 42

For rownames you can use:

>>t = table(a, b, 'RowNames', {'c'})t = a b __ __c 13 42

note that RowNames argument is a cell with a string for each row

In matlab, table has a specific meaning (see documentation) which is probably different from what you are used to in other langages.

For what you are trying to do, maybe you should consider to define a structure instead:

T = struct('a', 13, 'c', 42)

Check the struct documentation for more detail:

doc struct

As the MATLAB way to initialize a table with predefined column names seems particularly obscure to me, I am documenting it here.

Be aware that I use the cell2table function once to initialize the table.From then on, jobTable.FOO = ...; will enable you to create further columns FOO.

jobTable = cell2table([{'running'}, {'queued'}, {'finished'}]', 'VariableNames', {'state'});jobTable.rank = [0, 1, -1]';jobTable.workflowId = [0, 29039, 28000]';

You can transform the table into a struct, which is e.g. more useful for communication with java, by using: table2struct(jobTable).