フィルターのクリア

Variable name in a table with a space in it

79 ビュー (過去 30 日間)
Aaron
Aaron 2014 年 9 月 24 日
コメント済み: Steven Lord 2022 年 8 月 26 日
Hello,
how can I create a variable name in a table with a space character inside?
When I use
table(data,'RowNames',{'Row1'},'VariableNames',{'Variable X'})
I get the error message
'Variable X' is not a valid name.
How can I circumvent that?

採用された回答

Image Analyst
Image Analyst 2014 年 9 月 24 日
No. I don't know of any languages that can take spaces in variable names. The usual workaround is to use a different character, often an underline or dash.
  2 件のコメント
Aaron
Aaron 2014 年 9 月 24 日
You're probably right. I thought of it as just a name for some coloumn of a table, but in Matlab it seems to be more than that. It's a real identifier (if I'm using the right terms).
Image Analyst
Image Analyst 2014 年 9 月 24 日
Right. If you want, you could keep a separate array of strings, a cell array, with "friendly" names that use characters not allowed in variables of field/member names. See http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F, but of course the bookkeeping might be cumbersome.

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

その他の回答 (2 件)

Jack Arnoldi
Jack Arnoldi 2021 年 11 月 2 日
I encountered this problem myself and found out that I don't have a problem creating and working with a table that has spaces in its header.
If you want to add spaces in your header it works without giving me any error, for example I used:
MyTable.Properties.VariableNames = {'x' 'y' 'z' 'r x' 'r y' 'r z' 'f 1'};
And to use that I wrote:
MyTable.("f 1")
Note : I am working with Matlab 2021a
  2 件のコメント
Captain Karnage
Captain Karnage 2022 年 8 月 26 日
@Jack Arnoldi thanks for the additional post. I confirmed it works in MATLAB 2021a for me. I'm wondering if this was unsupported in the versions available when this was originally asked?
Steven Lord
Steven Lord 2022 年 8 月 26 日
Support for table and timetable variable names containing spaces was added in release R2019b. See the Release Notes for more information.

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


Francesco Tricarico
Francesco Tricarico 2020 年 10 月 23 日
Let me add some tips in addition to the Image Analyst accurate answer. I hope the following will help someone (working on a R2019b).
MATLAB R2019b accepts variable spaced names but it returns an error trying to access the variable directlty from the Variables tab in the GUI. For example both variables of this table
T = table({ones(8)},{1},'VariableNames',["variable 1", "variable 2"]);
are shown in the Variables tab:
Clicking on variable 2, the variable is not visible:
By the way, the variable content is in memory and becomes visible in the Command Window, prompting
T{1,1}{1}
i.e. accessing to the table cell with cell access method (MATLAB use structure access method when clicking on a table cell in the Variables tab).
A poor workourand is avoiding spaces and using Variable Descriptions (well documented in the official help) table property:
T = table({ones(8)},{1},'VariableNames',["variable_1", "variable_2"]);
T.Properties.VariableDescriptions = ["variable 1", "variable 2"];
summary(T)
Francesco

カテゴリ

Help Center および File ExchangeTables についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by