MATLAB Answers

Having trouble using the built in table function

1 ビュー (過去 30 日間)
AStar
AStar 2019 年 11 月 27 日
Commented: Rena Berman 2019 年 12 月 12 日
I have a large data set consiting of temperatures between 1984-2016 in Canada. I have calculated the mean, mode,min, max and standard deviation of this data and I am trying to display my results in a table using the built in table function. The table function works and it creates a table for me with my data the only problem is the data is not going into column format even though I had transposed my vectors before calling them in the table function. The "Mean T" corresponds with the first column in the month category and the first column in the min category and so on. The "ExtMax T" corresponds with the 2nd column of the month category and the second column of the min category and so on. I want these to all be vertical. So all of the "Mean T" then all of the "ExtMax T" under that and so on with the rest of the table. Does anyone know how i cal solve this? Thanks!
variable_names = ["Mean T","Mean T","Mean T","Mean T","Mean T","Mean T","Mean T","Mean T","Mean T","Mean T","Mean T" "Mean T"...
"ExtMax T","ExtMax T","ExtMax T","ExtMax T","ExtMax T","ExtMax T","ExtMax T","ExtMax T","ExtMax T","ExtMax T","ExtMax T","ExtMax T"...
"ExtMinT","ExtMin T","ExtMin T","ExtMin T","ExtMin T","ExtMin T","ExtMin T","ExtMin T","ExtMin T","ExtMin T","ExtMin T","ExtMin T"]';
month = (["Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec" ...
"Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec" ...
"Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"])';
min = ([jan_min1 feb_min1 mar_min1 apr_min1 may_min1 jun_min1 jul_min1 aug_min1 sep_min1 oct_min1 nov_min1 dec_min1 ...
jan_min2 feb_min2 mar_min2 apr_min2 may_min2 jun_min2 jul_min2 aug_min2 sep_min2 oct_min2 nov_min2 dec_min2 ...
jan_min3 feb_min3 mar_min3 apr_min3 may_min3 jun_min3 jul_min3 aug_min3 sep_min3 oct_min3 nov_min3 dec_min3])';
mean = ([jan_mean1 feb_mean1 mar_mean1 apr_mean1 may_mean1 jun_mean1 jul_mean1 aug_mean1 sep_mean1 oct_mean1 nov_mean1 dec_mean1...
jan_mean2 feb_mean2 mar_mean2 apr_mean2 may_mean2 jun_mean2 jul_mean2 aug_mean2 sep_mean2 oct_mean2 nov_mean2 dec_mean2...
jan_mean3 feb_mean3 mar_mean3 apr_mean3 may_mean3 jun_mean3 jul_mean3 aug_mean3 sep_mean3 oct_mean3 nov_mean3 dec_mean3])';
max = ([jan_max1 feb_max1 mar_max1 apr_max1 may_max1 jun_max1 jul_max1 aug_max1 sep_max1 oct_max1 nov_max1 dec_max1...
jan_max2 feb_max2 mar_max2 apr_max2 may_max2 jun_max2 jul_max2 aug_max2 sep_max2 oct_max2 nov_max2 dec_max2 ...
jan_max3 feb_max3 mar_max3 apr_max3 may_max3 jun_max3 jul_max3 aug_max3 sep_max3 oct_max3 nov_max3 dec_max3])';
median = ([jan_median1 feb_median1 mar_median1 apr_median1 may_median1 jun_median1 jul_median1 aug_median1 sep_median1 oct_median1 nov_median1 dec_median1 ...
jan_median2 feb_median2 mar_median2 apr_median2 may_median2 jun_median2 jul_median2 aug_median2 sep_median2 oct_median2 nov_median2 dec_median2 ...
jan_median3 feb_median3 mar_median3 apr_median3 may_median3 jun_median3 jul_median3 aug_median3 sep_median3 oct_median3 nov_median3 dec_median3])';
modes = [jan_mode1 feb_mode1 mar_mode1 apr_mode1 may_mode1 jun_mode1 jul_mode1 aug_mode1 sep_mode1 oct_mode1 nov_mode1 dec_mode1...
jan_mode2 feb_mode2 mar_mode2 apr_mode2 may_mode2 jun_mode2 jul_mode2 aug_mode2 sep_mode2 oct_mode2 nov_mode2 dec_mode2...
jan_mode3 feb_mode3 mar_mode3 apr_mode3 may_mode3 jun_mode3 jul_mode3 aug_mode3 sep_mode3 oct_mode3 nov_mode3 dec_mode3]';
std = ([jan_std1 feb_std1 mar_std1 apr_std1 may_std1 jun_std1 jul_std1 aug_std1 sep_std1 oct_std1 nov_std1 dec_std1...
jan_std2 feb_std2 mar_std2 apr_std2 may_std2 jun_std2 jul_std2 aug_std2 sep_std2 oct_std2 nov_std2 dec_std2 ...
jan_std3 feb_std3 mar_std3 apr_std3 may_std3 jun_std3 jul_std3 aug_std3 sep_std3 oct_std3 nov_std3 dec_std3])';
Tab=table(variable_names,month,min,mean,median,modes,max,std)

  3 件のコメント

per isakson
per isakson 2019 年 11 月 27 日
I get the impression that you try to achieve something that's not possible with the Matlab table. What do you mean by "category"?
Are variable_names and month meant to be column and row names, respectively? Why the triplets?
AStar
AStar 2019 年 11 月 27 日
I have seen a colleague use the table function to accomplish what I am trying to do, but for some reason its not working for me. By category I just mean the label on the table. I am trying to get my table to look like the attached photo.
Screen Shot 2019-11-26 at 10.38.34 PM.png
Rena Berman
Rena Berman 2019 年 12 月 12 日
(Answers Dev) Restored edit

サインイン to comment.

採用された回答

per isakson
per isakson 2019 年 11 月 27 日
%%
variable_names = ["variable","month","min","mean","median","mode","max","std"];
variable = [ repmat("Mean T",1,12),repmat("ExtMax T",1,12),repmat("ExtMin T",1,12), ];
month_ = repmat( ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],1,3);
%%
T = table( reshape( variable, [],1 ) ...
, reshape( month_, [],1 ) ...
, rand(36,1) ...
, rand(36,1) ...
, rand(36,1) ...
, rand(36,1) ...
, rand(36,1) ...
, rand(36,1) ...
, 'VariableNames', variable_names );
>> T
T =
36×8 table
variable month min mean median mode max std
__________ _____ _________ ________ ________ ________ ________ ________
"Mean T" "Jan" 0.059403 0.98793 0.93573 0.6537 0.025135 0.80445
"Mean T" "Feb" 0.31581 0.17043 0.45789 0.93261 0.42111 0.9861
"Mean T" "Mar" 0.77272 0.25779 0.24048 0.16351 0.1841 0.029992
"Mean T" "Apr" 0.69643 0.3968 0.7639 0.9211 0.72578 0.53566
"Mean T" "May" 0.12533 0.073995 0.75933 0.79466 0.37036 0.087077
"Mean T" "Jun" 0.13015 0.6841 0.74065 0.57739 0.84156 0.80209
"Mean T" "Jul" 0.092352 0.40239 0.74369 0.44004 0.73423 0.98914
"Mean T" "Aug" 0.0078203 0.98284 0.10592 0.25761 0.57103 0.066946
"Mean T" "Sep" 0.42311 0.40218 0.68156 0.75195 0.17686 0.9394
"Mean T" "Oct" 0.65557 0.62067 0.46326 0.22867 0.95738 0.018178
"Mean T" "Nov" 0.72292 0.15437 0.21216 0.064187 0.26532 0.68384
"Mean T" "Dec" 0.53121 0.38135 0.098519 0.76733 0.92458 0.78374
"ExtMax T" "Jan" 0.10882 0.16113 0.82357 0.6712 0.22377 0.53414
"ExtMax T" "Feb" 0.63177 0.75811 0.17501 0.71521 0.37356 0.88536
"ExtMax T" "Mar" 0.1265 0.87111 0.16357 0.64206 0.0875 0.899
"ExtMax T" "Apr" 0.1343 0.35078 0.66599 0.41905 0.64012 0.62594
"ExtMax T" "May" 0.098594 0.68554 0.89439 0.39076 0.18062 0.13787
"ExtMax T" "Jun" 0.14203 0.29415 0.51656 0.81614 0.045051 0.2178
"ExtMax T" "Jul" 0.16825 0.53063 0.7027 0.31743 0.72317 0.18214
"ExtMax T" "Aug" 0.19625 0.83242 0.15359 0.81454 0.34744 0.04182
"ExtMax T" "Sep" 0.31748 0.59749 0.95346 0.78907 0.66062 0.10694
"ExtMax T" "Oct" 0.31643 0.33531 0.54088 0.85226 0.38387 0.61644
"ExtMax T" "Nov" 0.21756 0.29923 0.67973 0.50564 0.62735 0.93966
"ExtMax T" "Dec" 0.25104 0.45259 0.036563 0.63566 0.02165 0.35446
"ExtMin T" "Jan" 0.89292 0.42265 0.8092 0.95089 0.91057 0.41063
"ExtMin T" "Feb" 0.70322 0.35961 0.74862 0.44396 0.80056 0.98435
"ExtMin T" "Mar" 0.55574 0.55832 0.12019 0.060019 0.74585 0.94558
"ExtMin T" "Apr" 0.18443 0.74255 0.52505 0.86675 0.81311 0.67664
"ExtMin T" "May" 0.21203 0.42433 0.32583 0.63119 0.38331 0.9883
"ExtMin T" "Jun" 0.077347 0.42936 0.54645 0.35507 0.61728 0.76683
"ExtMin T" "Jul" 0.9138 0.12487 0.39888 0.997 0.57549 0.3367
"ExtMin T" "Aug" 0.70672 0.024434 0.41509 0.22417 0.53005 0.66238
"ExtMin T" "Sep" 0.55779 0.29019 0.18074 0.65245 0.27507 0.24417
"ExtMin T" "Oct" 0.31343 0.31752 0.25539 0.60499 0.24863 0.29551
"ExtMin T" "Nov" 0.1662 0.65369 0.020536 0.38725 0.45164 0.68018
"ExtMin T" "Dec" 0.6225 0.95694 0.92368 0.14219 0.22771 0.52785
>>

  0 件のコメント

サインイン to comment.

More Answers (1)

Image Analyst
Image Analyst 2019 年 11 月 27 日
Do not use min, max, std, etc. as variable names since those are the names of built in functions. Use other names, such as adding Vec to them.
What happens if you transpose your variables and use the VariableNames option:
Tab=table(month', minVec', meanVec', medianVec', modes', maxVec', stdVec', 'VariableNames', variable_names)

  2 件のコメント

per isakson
per isakson 2019 年 11 月 27 日
Am I missing something?
Never use names of functions as names of variables. That's a good role that shields against confusion. I agree on that. However, it doesn't hurt to use names of functions as names of variables in table or as names of fields in struct
y = ones(4,1);
x = zeros(4,1);
table( x, y, 'VariableNames',["min","max"] )
ans =
4×2 table
min max
___ ___
0 1
0 1
0 1
0 1
Image Analyst
Image Analyst 2019 年 11 月 27 日
Yes, per, you are. As we all are, because the original poster deleted the original message. You are correct that you can have variable names like min and max. However, in the original, now deleted question, AStar had variables called min, mean, max, std, etc. -- the same as the function names. It wasn't just the variable names like you see now.
It used to say something like
Tab=table(variable_names, month, min, mean, median, mode, max, std)
where his variable names were the same as function names. I cautioned him against doing that. However I left his variable_names cell array intact with the names he and you had (the function names) so I didn't change the field names (column headers).
He forgot to delete one comment, so since that's all we have now, I'm going to put it below so at least that will still be here if he deletes that too:
I have seen a colleague use the table function to accomplish what I am trying to do, but for some reason its not working for me. By category I just mean the label on the table. I am trying to get my table to look like the attached photo.
0000 Screenshot.png

サインイン to comment.

サインイン してこの質問に回答します。

タグ


Translated by