extract all values within a if loop to be stored to be used in a figure

1 visualización (últimos 30 días)
%Source Level
if 0<i && i<=PnC(1)
SL = 200;
else
SL = 210;
end
This is the relevant code, i want both vaues of SL to be stored and used in a plot, as at the moment only the value of 210 is being stored. the if statement is a time period to when SL = 200, and after this period the remaining part of the event is of SL = 210.
%Time
T = 5400;
INT = 900;
TINT = 0:INT:T;
%Number of Positions and Impacts
In = [50, 60, 75, 375, 420, 360];
Pn = sum(In);
PnC = cumsum(In);
n = numel(TINT);
TVpieces = cell(1, n-1);
for iter = 1:n-1
Inc = INT/In(iter);
TVpieces{iter} = TINT(iter):Inc:(TINT(iter+1)-Inc);
end
TV = [TVpieces{:}];
for i = 1:Pn
%Source Level
if 0<i && i<=PnC(1)
SL = 200;
else
SL = 210;
end
end
%RECEIVE LEVEL MODEL
figure
% set(plot(TV,RL,'.'),'markersize',3)
% hold on;
set(plot(TV,SL,'.'),'markersize',3)
% hold on;
% set(plot(TV,RLS,'.'),'markersize',3)
xlabel('Time (s)')
ylabel('Decibels (dB)')
legend({'Individual Strike Recieve Level (dB re μPa^2s)','Individual Strike Source Level (dB re μPa^2s-m)'},'Location','north')
%'Cumalative SEL Level (dB re μPa^2s)'
When we run this code, the graph produced only plots the values for SL at 210, whereas during the condition of if 0<i && i<=PnC(1) i would like the plot to be of 200, once this is complete SL is 210 based on its time. the graph should show 2 straight horizontal lines, when SL = 200 between 0<i && i<=PnC(1) and SL = 210 between PnC(1)<i && PnC(end).

Respuesta aceptada

Jan
Jan el 17 de Ag. de 2022
Editada: Jan el 17 de Ag. de 2022
The code overwrite SL in each iteration. Store it as a vector instead:
SL = zeros(1, Pn)
for i = 1:Pn
if 0<i && i <= PnC(1)
SL(i) = 200;
else
SL(i) = 210;
end
end
or shorter:
SL = 200 + 10 * ((1:Pn) <= PnC(1));
% or
SL = repmat(200, 1, Pn);
SL(PnC:Pn) = 210;
Note that for the index i running from 1 to Pn, checking 0 < i is useless.
  2 comentarios
Adil Saeed
Adil Saeed el 17 de Ag. de 2022
thank you, much appreciated. about your last note, checking for 0 < i how would you change it?
Jan
Jan el 17 de Ag. de 2022
I'd simply omit the test of 0 < i, because this cannot happen in the loop.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by