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).
댓글 수: 0
채택된 답변
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.
댓글 수: 2
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!