Changing names and removing quotes in row2vars output table

I have generated an output table with iteration data from several equations:
I transposed the table for greater viewing comfort with splitvars and row2vars:
But now, I have some new row and column names, plus those ugly quote marks.
Now: how to replace Var1, Var2, Var3 etc with 'h', 'A', 'P', 'R', 'Energy E' etc.?
How to get rid of that 'Original/VariableNames' in top left corner?
And how to remove quotations from data?

 채택된 답변

dpb
dpb 2022년 1월 25일

0 개 추천

As always, if you would show the code you used to create the table, it would make our job much easier...
The table command and friends (like array2table) have the named input parameter 'VariableNames' that you can use to create the names you wish on creation, or you can use the tableName.Properties.VariableNames property to assign the names after creation.
However, from the content of the table, it appears you did not use the "ReadVariableNames,1" parameter in order for whichever of the functions you did use that would have read the first record as variable names and given you those automagically.
Doing that would then let all the other data be treated as numeric and then being numbers, the apostrophes will go away.
The apostrophes are a display feature of MATLAB command window and cannot be removed from fields that are strings; they are the visual clue as to what type of variable is in the given column; a cellstr and char string variable are also shown as either in curly braces or single quotes. But, if you do the above and fix it to read the first record as variable names, then they will be seen as numeric and all will be well in the world.

댓글 수: 4

KarolN
KarolN 2022년 1월 26일
편집: KarolN 2022년 1월 26일
@dpb your solution sounds like my salvation! Could you please give me a hint where should I insert 'VariableNames' or "ReadVariableNames,1" in the code, that creates my table, below ?
Parameters = ["h";"A";"P";"R";"v";"If";"Energy E"];
fromSection = [h; A; P; R; v; If; EnergiaE];
PP = table(Parameters,fromSection)
WaterFlow = splitvars(PP);
WaterFlow = rows2vars(WaterFlow)
dpb
dpb 2022년 1월 26일
편집: dpb 2022년 1월 26일
The "ReadVariableNames" named parameter only comes into play when you're reading a file; we weren't able to see from whence the data came so that was a calculated guess. It's still not clear from the above code how you got the variables that make up the array fromSection; if they are via calculations, then that would be something other than readtable; but if those are from other code reading one or more files, then it might be more direct to use readtable and built the table directly--but, we can't tell without more context.
But, above you listed the variable names as a variable in the input argument list for table which is why they're the first line/record in the table. By default, table will use the variable names as the table variables; hence you don't need the Parameters variable at all unless you're adamant about using a variable name containing a blank in it for energy -- the practice of which I'd strongly recommend against; it just creates a real nuisance in having to type it and surround it by quotes to use it.
tPP=table(h(:), A(:), P(:), R(:), v(:), If(:), EnergiaE(:));
will build the table with the default table-variable names the same as the variables themselves; if you really, really insist on the other list, then
tPP=table(h(:), A(:), P(:), R(:), v(:), If(:), EnergiaE(:), ...
'VariableNames',Parameters);
NB: the (:) is to ensure each variable is a column vector; your use of the semicolon in combining them into the array would imply they were row vectors initially.
See the documentation tor table, read about all the inputs and look at the examples given.
After that, ditch the next two lines entirely; once you have the data in the table, reference and use the variables in it instead of making duplicate copies of the same thing.
Read the section on tables in the documentation; there's a whole section devoted to addressing table variables and another on processing table data with varfun, rowfun and friends.
@dpb Thank you very much for solving my problem!
Just for your curiosity:
Parameters variable is neccessary, because it is the only way to have greek letters as table names.
fromSection was a name of a vector, which gathers the output from 9 equations. When I transposed the table MATLAB had to split it to preserve the logic of the output I guess
"... because it is the only way to have greek letters as table names."
Yech! Another terrible thing to have to then enter in order to use the data alll for a little visual bling.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

릴리스

R2020b

질문:

2022년 1월 25일

댓글:

dpb
2022년 1월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by