How to create a table of 3 variables ?
27 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Aleksandra Ksiezyk
el 17 de En. de 2020
Comentada: Aleksandra Ksiezyk
el 22 de En. de 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 ?
0 comentarios
Respuesta aceptada
Adam Danz
el 17 de En. de 2020
Editada: Adam Danz
el 20 de En. de 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 comentarios
Adam Danz
el 20 de En. de 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')
Más respuestas (1)
Aleksandra Ksiezyk
el 19 de En. de 2020
2 comentarios
Adam Danz
el 19 de En. de 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
el 20 de En. de 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
Ver también
Categorías
Más información sobre Timetables en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!