4
\$\begingroup\$

I use this Matlab code:

load ~/emailAnalysis/results.txt
results=results(size(results,1)-400:size(results,1),:)
temp = results(:,3)-1238370000;
h=plot(temp,smooth(results(:,1)),':b')
set(h,'LineWidth',1)
ylim([0 80])
xlim([max(temp)-(86400*7),max(temp)-1])
set(gca,'XGrid','on')
ylabel('Emails')
hold on
i=plot(temp,smooth(results(:,4)),'r')
j=plot(temp,smooth(results(:,5)),'-g')
k=plot(temp,smooth(results(:,6)),'m')
xlim([max(temp)-(86400*7),max(temp)-1])
set(gca,'XTick',[1:86400:(86400*max(temp))+1])
set(gca,'XTickLabel',['Mon';'Tue';'Wed';'Thu';'Fri';'Sat';'Sun'])
set(j,'LineWidth',2)
set(h,'LineWidth',1)
set(i,'LineWidth',2)
set(k,'LineWidth',2)
xlabel('Time')
title('Size of inbox over time (seven days)')
print -r3000 -djpeg /XXXX/inbox7day.jpeg
hold off

to generate this graph:

graph

from data like:

34 2012年01月21日 1327152611 5 16 10
32 2012年01月21日 1327154411 5 14 9
32 2012年01月21日 1327156209 5 14 9
34 2012年01月21日 1327158012 5 14 9
34 2012年01月21日 1327159808 5 15 9
34 2012年01月21日 1327161611 5 15 9
34 2012年01月21日 1327163406 5 15 9
33 2012年01月21日 1327165211 5 13 9
34 2012年01月21日 1327167011 5 13 9
31 2012年01月21日 1327168810 4 12 8

but it's a little slow, and also probably not very elegant (it's my first use of Matlab) I'm interested in any tips for best practice, tips for speeding it up, or examples of how I might do the same things in R...

palacsint
30.3k9 gold badges82 silver badges157 bronze badges
asked Jan 21, 2012 at 18:10
\$\endgroup\$

1 Answer 1

4
+25
\$\begingroup\$
clear all;
load('results.txt');
time = results(:,3) - 1238370000;

There's no need to plot all of the points if you're going to restrict the range to the past week. So I'd suggest you find the index where the time is less than a week ago and limit your plotting to that period to reduce complexity

firstTimeIndex = find(time >= (max(time) - 86400*7), 1);

Also note that you can use 'end' instead of 'size(results,1)'

results = results(max(1,firstTimeIndex-1):end, :);
clf;
h = plot(time,smooth(results(:,1)),':b');
hold on;

Line properties like linewidth can be specified at the time of plotting

plot(time, smooth(results(:,4)), 'r', 'linewidth', 2);
plot(time, smooth(results(:,5)), 'g', 'linewidth', 2);
plot(time, smooth(results(:,6)), 'm', 'linewidth', 2);
xlabel('Time');
ylabel('Emails');
xlim([max(time)-(86400*7) max(time)-1]);
ylim([0 80]);
set(gca,'XGrid','on');
set(gca,'XTickLabel',['Mon';'Tue';'Wed';'Thu';'Fri';'Sat';'Sun']);
title('Size of inbox over time (seven days)');

I'm not sure why you are setting the resolution to 3000 dpi? Maybe you could elaborate on what you're using this for, but if your goal is high resolution and low storage size then you might like to use -dmeta for a metafile on windows, or -deps for an encapsulated postscript on unix

print -djpeg inbox7day.jpeg;

I'm not sure what you are trying to achieve with the 'XTick' property, but the vector you're passing in is extremely large (~100 million elements) which is why the code is running so slow. I'm guessing that you don't want 100 million ticks?

answered Feb 11, 2012 at 21:24
\$\endgroup\$
1
  • \$\begingroup\$ Awesome. Particularly that I hadn't noticed the problem with the xticks :) \$\endgroup\$ Commented Feb 15, 2012 at 19:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.