필터 지우기
필터 지우기

How to write a table with mixed values?

조회 수: 23 (최근 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 CenterFile Exchange에서 Tables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by