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

1 次查看(过去 30 天)
%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).

采纳的回答

Jan
Jan 2022-8-17
编辑:Jan 2022-8-17
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by