I get an error when I try to add variable names to a table

31 visualizzazioni (ultimi 30 giorni)
My tabble correctly prints the results when I am not trying to add the variable names, but when I add the variable names the following error occurs: t = setVarNames(t,vnames); % error if invalid, duplicate, or empty
I am trying to use this line
T = table (fPv', FPv', sPv', mPv', gv', dEv', dIv', dPv', beta1', aEv', aIv', aPv', Ev', Iv', Pv', uEv', uIv', uPv', gammav', 'VariableNames', {'fP','FP','sigma P','mu P','g','dE','dI','dP','beta','alpha E','alpha I','alpha P','E','I','P','uE','uI','uP','tol'})
Also if anyone can help me, this prints a table with the results beings put in columns under the variable, is there a way of printing them as rows to the right of the variable name?

Risposte (1)

Peter Perkins
Peter Perkins il 13 Mar 2015
The error that I get is
Error using matlab.internal.tableUtils.makeValidName (line 36)
'sigma P' is not a valid variable name.
Error in setVarNames (line 50)
[newnames,wasMadeValid] = matlab.internal.tableUtils.makeValidName(newnames,exceptionMode); % will warn if mods are made
Error in table (line 305)
t = setVarNames(t,vnames); % error if invalid, duplicate, or empty
which pretty much explains the problem: "'sigma P' is not a valid variable name." Variable names in a table must be valid MATLAB identifiers, so embedded spaces are not allowed. If you were reading data from a file, readtable would assume you wanted it to automatically fix such problems. But you've passed in these names explictly, so the burden is on you. 'SigmaP' or 'sigma_P' would be fine, similarly for the others.
Tables also support row names. That might get you the horizontal layout you're looking for. If your data are all numeric, I recommend combining them into one big matrix and using array2table to create the table, and you can try out either orientation easily. If your data are not all numeric, then this may not work, because the data in a table must be homogeneous within each variable, i.e., in the vertical direction.

Categorie

Scopri di più su Tables in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by