How to make this code more efficient?

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')

5 Comments

I dont think there is anything wrong with that code. Plotting is just inheritnly slow.
I tend not to plot multiple lines with a single plot() call just to help with readability.
If this code were plotting lots of data I think you could make the figure invisible, plot the data then make the figure visible at the end, this might be a bit faster but probably never really worth the effort.
I agree with Chris. Idomatically, the code is fine. As a plotting/post processing script, it's pretty clear what it does and easy to change/tweak, which is the point. Chris' suggestion of splitting plot commands onto separate lines (you'd use hold on) after the first plot command to each subplot to keep them onscreen is a good improvement. I'd also suggest grabbing the plot or axis handles (e.g. ax1 = subplot(2,2,1)). They make it much simpler to change properties like fonts or line styles after plotting.
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.]
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.
Thanks all for the suggestions. I would try to improve on Chris's points.

Sign in to comment.

Answers (0)

Products

Release

R2020b

Tags

Asked:

on 26 Sep 2020

Community Treasure Hunt

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

Start Hunting!