MATLAB: How to add non-row-sized variable to table

table

How can I add a variable to a table if the variable has a different length than the number of rows in the table? One reason to do this if one of the variables is a matrix, here hourlyTemperature,
nDays = 7; % Number of table rows
hoursOfDay = 6:6:24; % Vector which we wish to add
nHours = length(hoursOfDay);
hourlyTemperature = rand(nDays, nHours); % Matrix, notionally temperature measured every 6 hours for 7 days
dailyAvgWind = rand(nDays,1); % Vector, notionally daily average wind speed for 7 days
dailyAvgRain = rand(nDays,1); % Vector, notionally daily average rainfall for 7 days
% Make example table
Weather = table(dailyAvgWind, dailyAvgRain, hourlyTemperature, ...
'VariableNames', {'dailyAvgWind', 'dailyAvgRain', 'hourlyTemperature'});
I want to add hoursOfDay to the table because it is useful for interpreting hourlyTemperature. However since its length differs from the number of table rows (nDays), how do I do this?

Best Answer

Try this:
Weather = table(dailyAvgWind, dailyAvgRain, hourlyTemperature, repmat(hoursOfDay, [nDays, 1]), ...
'VariableNames', {'dailyAvgWind', 'dailyAvgRain', 'hourlyTemperature', 'HoursOfDay'});