필터 지우기
필터 지우기

how to create one row table

조회 수: 30 (최근 30일)
MAHMOUD ALZIOUD
MAHMOUD ALZIOUD 2017년 10월 19일
댓글: MAHMOUD ALZIOUD 2017년 10월 19일
Hello guys, I am trying to create a one row table, I have 19 variables (called them z) and their values (called them y), I wrote this code to display the results in an excel sheet. but the values in the table didn't come under the variable names, they all came in one line which is not a table !!! how can i fix this please? the code is :
z={'Month' 'Card_Name' 'Total_Volume' 'Truck_Volume' 'Sum_Class_1 ' 'Sum_Class_2' 'Sum_Class_3' 'Sum_Class4_' 'Sum_Class_5' 'Sum_Class_6' 'Sum_Class_7' 'Sum_Class_8' 'Sum_Class_9' 'Sum_Class_10' 'Sum_Class_11' 'Sum_Class_12' 'Sum_Class_13' 'Sum_Class_14' 'Sum_Class_15'};
y=[unique(str2num(Month_of_Data)), unique(str2num(Station_ID)), TotalV, TruckV, Sum_Class_1, Sum_Class_2, Sum_Class_3, Sum_Class_4, Sum_Class_5, Sum_Class_6, Sum_Class_7, Sum_Class_8, Sum_Class_9, Sum_Class_10, Sum_Class_11, Sum_Class_12, Sum_Class_13, Sum_Class_14, Sum_Class_15];
T=table(z,y)
filename = 'ourData2.xlsx';
writetable(T,filename,'Sheet',1,'Range','D1')

채택된 답변

Cam Salzberger
Cam Salzberger 2017년 10월 19일
Hello Mahmoud,
There are two issues going on here. One is that to specify the column names for a table, you want to provide them as part of a name-value pair argument to table:
T = table(..., 'VariableNames', varNames);
The other is that MATLAB doesn't automatically know that you want each column of your array in a separate variable. Having a full matrix in one column of a table is a viable storage of data. "table" requires that the different columns are provided as separate variables.
To separate it out easily, you can convert it to a cell array, and then use {:} to provide the different cells as individual variables. It's a common trick in other instances.
z = {'a' 'b'};
y = rand(1,2);
yc = num2cell(y, 1);
T = table(yc{:}, 'VariableNames', z)
-Cam
  댓글 수: 6
MAHMOUD ALZIOUD
MAHMOUD ALZIOUD 2017년 10월 19일
Now I understood, thank you very much Mr Cam for your help
MAHMOUD ALZIOUD
MAHMOUD ALZIOUD 2017년 10월 19일
It worked, thank you guys very much

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by