how to create one row table

30 次查看(过去 30 天)
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 个)

标签

Community Treasure Hunt

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

Start Hunting!

Translated by