Table array with numerical row names

18 ビュー (過去 30 日間)
Ken
Ken 2018 年 3 月 29 日
コメント済み: Peter Perkins 2020 年 7 月 28 日
I'm trying to create a table array where the row names are a vector of doubles, but row names are required to be a cell array of strings. How would someone do this?

採用された回答

Guillaume
Guillaume 2018 年 3 月 29 日
Convert your numbers to a cell array of char array (strings are not supported strangely). If the vector of numbers is all integers:
rownames = compose('%d', yourvector);
If it's real numbers then specify the appropriate format string in compose or matlab let do its thing with:
rownames = cellstr(string(yourvector));
Then create your table whichever way you were going to use, e.g.:
t = array2table(somearray, 'RowNames', rownames)

その他の回答 (3 件)

Sean de Wolski
Sean de Wolski 2019 年 1 月 28 日
編集済み: Sean de Wolski 2019 年 1 月 28 日
You could use matlab.lang.makeValidName to make valid names for each one. However, for the numeric case, this just adds x so you could do that directly and choose another letter or word, perhaps r or row:
matlab.lang.makeValidName(string(1:10))
ans =
1×10 string array
"x1" "x2" "x3" "x4" "x5" "x6" "x7" "x8" "x9" "x10"
Or
"r"+(1:10)
  2 件のコメント
Emiliano Alexander Carlevaro
Emiliano Alexander Carlevaro 2020 年 1 月 15 日
Mmmm... It doesn't seem to work anymore...
>> matlab.lang.makeValidName(string(1:10))
Error using matlab.lang.makeValidName (line 72)
First input must be a character vector or cell vector of character
vectors.
Walter Roberson
Walter Roberson 2020 年 1 月 15 日
Emiliano Alexander Carlevaro which release are you using? And have you accidentally defined a variable named string ?

サインインしてコメントする。


Elias Gule
Elias Gule 2018 年 3 月 29 日
Convert your vector of strings to a cell array of strings.
v = 1 : 20; % replace with your vector
row_names = arrayfun(@num2str,v,'uni',0);
  1 件のコメント
Guillaume
Guillaume 2018 年 3 月 29 日
編集済み: Guillaume 2018 年 3 月 29 日
strings are actually not supported for row names. The cell array must contain char vectors
Note that the above does create a cell array of char vectors, not strings.

サインインしてコメントする。


Peter Perkins
Peter Perkins 2018 年 3 月 29 日
The variable names in a table not only need to be text, they also need to be valid MATLAB identifiers (as do the field names in a struct). So while you might think to do this ...
>> t = table([1;2;3],[4;5;6],'VariableNames',{'1' '2'})
Error using table (line 307)
'1' is not a valid variable name.
... you can't. But hang on a minute - create this table:
>> t = table([1;2;3],[4;5;6],'VariableNames',{'one' 'two'})
t =
3×2 table
one two
___ ___
1 4
2 5
3 6
Now index into it using the numbers you'd like to have as the variable names:
>> t(:,1:2)
ans =
3×2 table
one two
___ ___
1 4
2 5
3 6
>> t{:,1:2}
ans =
1 4
2 5
3 6
and even
>> t.(1)
ans =
1
2
3
What is it that you can't do right now, except have '1' displayed as the name?
  2 件のコメント
math man
math man 2019 年 1 月 28 日
For me, the idea is to have a set of Variable names which I can read in from elsewhere (perhaps a vector of 15 to 20 numbers). I'd then like to be able to refer to them. So ideally I'd have a set of variablesnames which I can call dynamically (NOT just assigning 'One', 'Two', etc.), and also which I can refer to for the purposes of Indexing, like in Excel.
so
numbervariablenames = otherarray(1,:)
t = table(somedata,'VariableNames',numbervariablenames)
and I want to be able to do:
Col = find(strcmp(t.VariableNames,num2str(SomeNumber)),1)
ColumnofData_Iwant = t{:,Col}
Note that the earlier solution by Guillame did not work for me, I get this error:
Error using table.init (line 401)
'2.96926' is not a valid variable name.
Error in array2table (line 64)
t = table.init(vars,nrows,rownames,nvars,varnames);
Thanks for any help!
Peter Perkins
Peter Perkins 2020 年 7 月 28 日
In one of the 2019 releases (I forget which), the requirement that table/timetable variable names be valid MATLAB identifiers was relaxed. So this
>> t = array2table(rand(5,3),'VariableNames',["1" "2 2" "(3)"])
t =
5×3 table
1 2 2 (3)
________ _______ _______
0.69875 0.47992 0.80549
0.19781 0.90472 0.57672
0.030541 0.60987 0.18292
0.74407 0.61767 0.23993
0.50002 0.85944 0.88651
is a legal table now. Of course, to access those variables, t.1 or t.2 2 isn't gonna work, so you need to do this:
>> t.("1")
ans =
0.69875
0.19781
0.030541
0.74407
0.50002

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by