How to create a table of 3 variables ?

26 vues (au cours des 30 derniers jours)
Aleksandra Ksiezyk
Aleksandra Ksiezyk le 17 Jan 2020
I have a matrix Thm = [hours,days,months], where hours is [1x24], days is [1x31] and months [1x12]. I would like to create a table of hours and days [hours, days] for each month. I was wondering if i could substitute a vector of months 1:12 to a vector of a string {'Jan','Feb',...}...Could anyone help me with this, please ?

Réponse acceptée

Adam Danz
Adam Danz le 17 Jan 2020
Modifié(e) : Adam Danz le 20 Jan 2020
New answer following your updated description.
You are asking to reorganize the array into tables by month and to name those tables according to the month. That would require dynamic variable naming which you should avoid doing at all cost (see the link).
I'm assuming you have the actual time stamps of each data point or that they can be computed. In that case it would be wise to form a timetable with a single column for temperature. See this comment below for a demo using your data.
That would make your calculations very easy.
--------------------------------------------------------------------------
Old answer (removed, obsolete).
  6 commentaires
Adam Danz
Adam Danz le 20 Jan 2020
Here's what it would look like if you organized your data into a timetable. See inline comments for details and execute the code line by line to see what it's doing.
Step 1 fill missing data with Nan
Columns of 0s in T_h_m indicate days that don't exist (ie, 30-Feb). Here we replace those columns of 0s with NaNs.
load('T_h_m'); % hours,days,months
% Replace full columns of 0s with NaNs
T_h_m(:,all(T_h_m == 0,1)) = NaN;
% Create vector in order of hours, days, months; remove NaNs.
Thm = T_h_m(~isnan(T_h_m));
% Sanity check: the length of Thm should be the number of days
% in a year (potentially including leap years)
if ~ismember(numel(Thm), [8760 8761])
error('T_h_m is missing data.')
end
Step 2 Convert to timetable
A starting date and time must be selected, the rest of the dates and times are computed.
% Create a timetable with known starting date
% Jan 1, 2019 00:00:00
startDate = datetime(2019,1,1,0,0,0);
timestamps = startDate + hours(0:numel(Thm)-1);
TT = timetable(timestamps(:), Thm);
% Look at the first few rows of the timetable
head(TT)
Step 3 Select a day to plot
Note the 2 different methods of selecting a day (choose 1). Then identify the rows of the timetable that belong to that day.
% Choose day (Method 1)
selectDay = datetime(2019,02,14); % 14 Feb 2019
% or you could choose day of year (Method 2)
dayOfYear = 45;
selectDay = TT.Time(1) + days(dayOfYear-1);
% Identify rows that belong to selected day
rowIdx = TT.Time >= selectDay & TT.Time < selectDay + days(1);
Step 4 Plot that day's data
I also show how to interpolate to seconds resolution (but I recommend using minutes instead).
% If you really need to interpolate that to second resolution
% you can do this (or use 'minutely' which would be better)
TTsec = retime(TT(rowIdx,:),'secondly','linear');
% Plot it
plot(TTsec.Time, TTsec.Thm, 'b-')
% Find the y-data (adapted from your code)
ii = 2;
idx = TTsec.Thm > ii-0.5 & TTsec.Thm < ii+0.5;
hold on
plot(TTsec.Time(idx), TTsec.Thm(idx), 'o')
Aleksandra Ksiezyk
Aleksandra Ksiezyk le 22 Jan 2020
thanks a lot.. !!!

Connectez-vous pour commenter.

Plus de réponses (1)

Aleksandra Ksiezyk
Aleksandra Ksiezyk le 19 Jan 2020
hi,
so i have created the attached .mat
like i said.. it is 3d (T_h_m[hours,days,months]) those are outdoor temp values. hourly values of each day per month.
i have already created plots for each month (i sent one as an example) using a for loop and for an overview i wanted to create tables but i was receiving all the time some errors :( and i do not know where i was doing mistakes
but, if i could ask one more question... what i must do also is to calculate a duration of a specified temperature range - that means : calculate how long the temperature of e.g. from 18.5 to 19.5 degrees lasted on the 10th of October. Do u have any idea how i could compute that ?
cheers,
Ola
  2 commentaires
Adam Danz
Adam Danz le 19 Jan 2020
I see now. Previously you described your variable as a matrix (see the 4th word in your question), not a a 3D array.
But this part is still unclear,
"I would like to create a table of hours and days [hours, days] for each month"
Does that mean you'll have 12 different tabes or do you want all of it in 1 table?
Adam Danz
Adam Danz le 20 Jan 2020
Aleksandra Ksiezyk 's answer moved here as comment.
ajc... sorry .. my mistake... yes, my idea was to created a for loop to get 12 tables with my value

Connectez-vous pour commenter.

Catégories

En savoir plus sur Timetables dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by