Borrar filtros
Borrar filtros

Conditional retime method in a timetable

6 visualizaciones (últimos 30 días)
Gabi
Gabi el 17 de Abr. de 2024
Comentada: Voss el 18 de Abr. de 2024
Hi,
I'm looking for a way to create a conditional retime (at a fixed fequency, let's say 1 Hz) method based on the actual values of a variable Var in a timetable T.
In the time table T attached::
if Var transitions from 0 -> 1, 0 -> 2, 1 -> 2 or 2 -> 1 use method next,
if Var transitions from 1 -> 0 or 2 -> 0 use method previous.
Is there a way?
Thanks in advance.

Respuesta aceptada

Voss
Voss el 17 de Abr. de 2024
Maybe something like this:
S = load('timetable.mat');
T2 = S.T2;
dt = 1;
t = seconds(T2.Time);
N = round((t(end)-t(1))/dt)+1;
ti = linspace(t(1),t(end),N).';
Vari = zeros(N,1);
for ii = 1:numel(t)-1
Vari(ti>t(ii) & ti<=t(ii+1)) = T2.Var(ii+logical(T2.Var(ii+1)));
end
% Another way to write that loop; may be easier to understand:
%
% for ii = 1:numel(t)-1
%
% idx = ti>t(ii) & ti<=t(ii+1);
%
% if T2.Var(ii+1) == 0 % transition ends with 0
% % (i.e., 0->0, 1->0, 2->0)
%
% Vari(idx) = T2.Var(ii); % use previous
%
% else % transition ends with 1 or 2
% % (i.e., 0->1, 0->2, 1->1, 1->2, 2->1, 2->2)
%
% Vari(idx) = T2.Var(ii+1); % use next
%
% end
%
% end
T2_retimed = timetable(seconds(ti),Vari, ...
'VariableNames',T2.Properties.VariableNames);
figure
plot(T2.Time,T2.Var,T2_retimed.Time,T2_retimed.Var)
legend({'Original','Retimed'},'Location','NorthWest')
  2 comentarios
Gabi
Gabi el 18 de Abr. de 2024
Thanks, it surely works.
Voss
Voss el 18 de Abr. de 2024
You're welcome!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by