How to create a table of 3 variables ?

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 ?

 Respuesta aceptada

Adam Danz
Adam Danz el 17 de En. de 2020
Editada: Adam Danz el 20 de En. de 2020

0 votos

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
Adam Danz el 19 de En. de 2020
Aleksandra Ksiezyk's answer moved here as a comment.
sorry that i am answering today...
hmmm... it does not work. it displays errors that my matrix should be 2D... but what i can see in your script, i think, i wanted to create sth else... i wanted to create a table 2x2 (horisontally - days and vertically - hours) for each month.
greetings,
Ola
Adam Danz
Adam Danz el 19 de En. de 2020
Please provide examples of your inputs and expected output (not descriptions, but actual examples of how those variables will look). Or you could attach a mat file with your inputs but I'll still need to see what your outputs should look like.
Aleksandra Ksiezyk
Aleksandra Ksiezyk el 20 de En. de 2020
Editada: Adam Danz el 20 de En. de 2020
oki, i will have a look into the link.
amm... no, i have created the timestep and i interpolate y values.... it is like this (now i am considering only a day):
y = T_h_m(:,1,4); % pierwszy dzien w kwietniu
x = (1:1:length(y))' ;
y = y(x) ;
x_int = linspace(0,24,24*60*60+1)' ;
y_int = interp1(x,y,x_int) ;
y_int = y_int(:) ;
for ii = round(min(y_int)):1:round(max(y_int))
idx = find(y_int>ii-0.5 & y_int<=ii+0.5) ;
y_values = y_int(idx) ;
x_values = x_int(idx) ;
vektor = [x_values y_values] ;
figure() % checking if the loop works
plot(x,y,vektor(:,1),vektor(:,2),'o')
end
and now i am thinking how i could get the x_values,... i was reading that one can use integral to calculate a curve but i think in my case it will be wrong, i must to subtract the greater from the smaller twice as it is in the pic
I have done it... the code is like that
y = T_h_m(:,1,4); % pierwszy dzien w kwietniu [24x1]
x = (1:1:length(y)) ; % [1x24]
y = y(x) ; %[24x1]
x_int = linspace(0,24,24*60*60) ; % [1x86400]
y_int = interp1(x,y,x_int) ; % [1x86400]
y_int = y_int(:) ; % [86400x1]
for ii = round(min(y_int)):1:round(max(y_int))
idx = find(y_int>ii-0.5 & y_int<=ii+0.5) ; % Temperaturintervall
y_values = y_int(idx) ;
x_values = x_int(idx) ;
[~,y_max_idx] = max(y_int) ; % Sprung
idx_steigen = idx(idx <= y_max_idx) ; % linke Seite - steigend
delta_t1 = x_int(idx_steigen(end)) - x_int(idx_steigen(1)) ; % Zeitdauer
idx_abnehmen = idx(idx >= y_max_idx) ; % linke Seite - abnehmend
delta_t2 = x_int(idx_steigen(end)) - x_int(idx_steigen(1)) ; % Zeitdauer
Zeitdauer = (delta_t1 + delta_t2) ;
vec(ii,:) = [ii Zeitdauer]
figure()
plot(x,y,x_values,y_values,'o')
end
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 el 22 de En. de 2020
thanks a lot.. !!!

Iniciar sesión para comentar.

Más respuestas (1)

Aleksandra Ksiezyk
Aleksandra Ksiezyk el 19 de En. de 2020

0 votos

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 comentarios

Adam Danz
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
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

Iniciar sesión para comentar.

Categorías

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by