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')
0 个评论
采纳的回答
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!