How to write a table with mixed values?

29 ビュー (過去 30 日間)
farzad
farzad 2020 年 4 月 20 日
編集済み: Walter Roberson 2020 年 4 月 22 日
Hi All
how do I write a Table, that the first row is only string. in the next rows, the first column number, the second and third string, and the next ones number ?

採用された回答

Walter Roberson
Walter Roberson 2020 年 4 月 20 日
You cannot do that with table() objects except by making everything string. You can define a Variable Description field for variables, but the description is only displayed if you use summarize(). table() objects are not designed for presentation purposes.
You also cannot do that with uitable() graphic objects except by making everything string. You can, though, define uitable ColumnName apart from the data values, which might be good enough for your purpose (do you just need the name displayed or do you need it to be stored with the table?)
  9 件のコメント
farzad
farzad 2020 年 4 月 22 日
thank you, I did not precisely get you. Shall you please indicate the code, for what I have in the question :
the first row is only string. in the next rows, the first column number, the second and third string, and the next ones number
Walter Roberson
Walter Roberson 2020 年 4 月 22 日
編集済み: Walter Roberson 2020 年 4 月 22 日
%we need some example data for the purpose of illustration. Do not include
%this in your real program
col1 = randi(9,[5,1]);
col2 = {'hello'; 'how'; 'are'; 'you'; 'today'};
col3 = {'My'; 'favorite'; 'color'; 'is'; 'pizza!'};
col4 = -randi(9,[5 1]);
col5 = randi(9,[5 1]).^2;
%table_without_header is standing in for your existing table before the first
%row has been added. Use your own table instead!
table_without_header = table(col1,col2,col3,col4,col5);
%and this is the cell of strings to put on the column. Use your own
header = {'Quantity', 'Category', 'Description', 'Selling Price', 'Number Sold'};
%now we go to work. This part you copy, changing variable names as needed
Tc = [header; table2cell(table_without_header)];
table_with_header = cell2table(Tc, 'VariableNames', table_without_header.Properties.VariableNames);
%we now have a table like you described, with the first row being string,
%and otherwise the first column being numeric, and the second and third
%columns being string, and the remaining columns being numeric
%now that we have it, write it out
writetable(table_with_header, 'FileNameToWriteTo.xlsx');
Note: You might want to use
'writevariablenames', false
Also, be sure to delete the output file FileNameToWriteTo.xlsx before you run the test.

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

その他の回答 (0 件)

カテゴリ

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