how to create one row table
30 views (last 30 days)
Show older comments
MAHMOUD ALZIOUD
on 19 Oct 2017
Commented: MAHMOUD ALZIOUD
on 19 Oct 2017
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 Comments
Accepted Answer
Cam Salzberger
on 19 Oct 2017
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 Comments
More Answers (0)
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!