how to create one row table

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')

 Accepted Answer

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

z = {'a' 'b'};
y = rand(1,2);
T = array2table(y, 'VariableNames', z);
Dear Mr Cam, Thank you for your help, since I am very new in MATLAB this is what I understood from your answer, but it still didnt work, it gives possibly unbalanced . this is the code:
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=num2cell(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);
filename = 'ourData2.xlsx';
writetable(T,filename,'Sheet',1,'Range','D1')
T = table(..., 'VariableNames', varNames);
yc = num2cell(y, 1);
T = table(yc{:}, 'VariableNames', z)
Thank you Mr Walter, I tried what you wrote but it still gives me unbalanced. I dont know what should I change
Cam Salzberger
Cam Salzberger on 19 Oct 2017
Edited: Cam Salzberger on 19 Oct 2017
Thanks Walter, I had forgotten about array2table.
Mahmoud, there are several issues with your code:
  1. You can't "writetable" before you define it. That should go at the end.
  2. My use of T = table(... was not meant to be taken literally. By "..." I meant "put something here that makes sense, and then put VariableNames etc. after all that". As I did in my example below that.
  3. When you define "y" initially, you're doing it with num2cell, but giving it a bunch of input arguments. I assumed you had already defined "y" as an array, and then you would convert it to a cell array with num2cell. Or don't bother, and just create your table with each of the different elements of y individually.
What I find to be the easiest way to learn to code in MATLAB is to start with a simple example. Run it, look at the output of each step, and understand it. Then start modifying it a little bit at a time, checking the output of each step to make sure it makes sense. Modify it piece by piece until it's close to what you want. That way if something goes wrong, you know which step it occurred at, and you have actually come to fully understand the code.
Now I understood, thank you very much Mr Cam for your help
It worked, thank you guys very much

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!