# How to make this code more efficient?

1 view (last 30 days)
suleiman abdullahi on 26 Sep 2020
I am new to MATLAB hence my poor use of it as you can see in this piece of code. I think a for loop would be handy here, so any help is appreciated.
% these are four time series to plotted against a set of signal data shown as accVert, accLat and accLong
newTime1 = (time >= 8 & time <= 11) ; % select data of specific time
newTime2 = (time >= 81 & time <= 86) ;
newTime3 = (time >= 124 & time <= 128) ;
newTime4 = (time >= 167 & time <= 172) ;
figure(3),clf(3)
subplot(2,2,1)
plot(time(newTime1),[accVert2(newTime1),accLong2(newTime1),accLat2(newTime1)],'linewidth',1.2)
xlabel('time, s')
ylabel('acceleration m/s^2')
title('acceleration at t = 8s and t = 12s')
subplot(2,2,2)
plot(time(newTime2),[accVert2(newTime2),accLong2(newTime2),accLat2(newTime2)],'linewidth',1.2)
xlabel('time, s')
ylabel('acceleration m/s^2')
title('acceleration at t = 81s and t = 86s')
subplot(2,2,3)
plot(time(newTime3),[accVert2(newTime3),accLong2(newTime3),accLat2(newTime3)],'linewidth',1.2)
xlabel('time, s')
ylabel('acceleration m/s^2')
title('acceleration at t = 124s and t = 128s')
subplot(2,2,4)
plot(time(newTime4),[accVert2(newTime4),accLong2(newTime4),accLat2(newTime4)],'linewidth',1.2)
xlabel('time, s')
ylabel('acceleration m/s^2')
title('acceleration at t = 167s and t = 172s')

Walter Roberson on 26 Sep 2020
Making the figure invisible would only help if you were executing this code at the command prompt instead of in a file, or if there were other calls going on that were doing drawnow() or pause() or figure() or uiwait() or waitfor() [all of which can trigger a screen redraw.]
Walter Roberson on 26 Sep 2020
Using a for loop could make the code more compact. However, it would only have a very small effect on code execution, due to the marginally faster parsing of slightly fewer lines of code. That marginally faster parsing in turn might be offset by the need to dynamically create the titles. The difference is not going to be large.
suleiman abdullahi on 27 Sep 2020
Thanks all for the suggestions. I would try to improve on Chris's points.

R2020b

### Community Treasure Hunt

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

Start Hunting!