Matlab Tips

editor: David Buttler
Return to Index



Matlab:

Contributed by: David Buttler

Scripts for Matlab
Creating input for Matlab
Creating usable output from Matlab
Graph types

Scripts for Matlab


If you use Matlab, try to write scripts to load a data file and create your output figures.  In the long run it will save a huge amount of time


Example script:
load groupTime.dat

d = groupTime;
x = d(:,1);
y1 = d(:,3);
y10 = d(:,5);
y100 = d(:,7);



figure(1);
plot (x,y1, 'gx-', x,y10, 'b+-', x, y100, 'r.-');

title('Cost for executing sentinels without grouping');
xlabel('Number of sentinels');
ylabel('Execution time (s)');
lo = legend('No grouping','Group size = 1','Group size = 10','Group size = 100', 0)
axis([0 100000 0 10])
set(get(gca, 'XLabel'), 'FontSize', 16);
set(get(gca, 'YLabel'), 'FontSize', 16);
set(lo, 'FontSize', 16);
set(get(gca, 'Title'), 'FontSize', 16);
set(gca, 'xtick', [10000 20000 30000 40000 50000 60000]);
set(gca, 'XTickLabel', {'10,000', '20,000', '30,000', '40,000', '50,000', '60,000'});
print -djpeg90 -r0 /net/hp71/students/buttler/www/WebCQ/groupThroughput.jpg
print -deps -r0 /net/hp71/students/buttler/WebCQ/paper/groupThroughput.eps
An example input file
 1000,1, 14611,10, 1883,100, 545,
2000,1, 30101,10, 3739,100, 709,
3000,1, 46638,10, 4743,100, 988,
4000,1, 55430,10, 7151,100, 1119,
5000,1, 68925,10, 8775,100, 1095,
6000,1, 78567,10, 10564,100, 1389,
7000,1, 92298,10, 10166,100, 1486,
8000,1, 105767,10, 11664,100, 1624,
9000,1, 115925,10, 13251,100, 2008,
10000,1, 125577,10, 15147,100, 2023,

Input:
So the next question is how you create a nice clean input file like the one above
1) generate appropriate logs from your execution or simulation that captures appropriate information
2) use grep, sed, and gawk to clean the logs
3) use David's database functions to process the logs: sort, join, average, min, max, group, and GroupBy

Of course you are asking "David, I know how to use grep, sed, and gawk, but how can I use those incredible database functions on my log files?"
Here you go, add the following lines to you tcsh shell initialization file:
setenv W ~buttler/workspace
mycat java -cp $W/Util:$W/JARS/jgl.jar edu.gatech.disl.fsdb.Cat
myjoin java -cp $W/Util:$W/JARS/jgl.jar edu.gatech.disl.fsdb.Join
mysort java -cp $W/Util:$W/JARS/jgl.jar edu.gatech.disl.fsdb.Sort
mysplit java -cp $W/Util:$W/JARS/jgl.jar edu.gatech.disl.fsdb.Split
group java -cp $W/Util:$W/JARS/jgl.jar edu.gatech.disl.fsdb.Group
groupby java -cp $W/Util:$W/JARS/jgl.jar edu.gatech.disl.fsdb.GroupBy

What is the difference between "group" and "groupby"?
group concatenates similar lines, groupby operates like the database operator

given the following datafile named data.dat
1000,1, 14611,
1000,10, 1883,
1000,100, 545,
executing the command
"group data.dat , 0"
produces
1000,1, 14611,10, 1883,100, 545,
"groupby data.dat , 0 sum 1"
produces
1000, 111
"groupby data.dat , 0 sum 1,2"
produces
1000, 111, 17039

groupby also supports the following operators: min, man, and avg


mycat operates like the standard cat command, except that it prepends the filename to each line of the file
mysort allows sorting on numbers and strings in ascending or descending order

For more information, use the source.  It is all there in CVS under the Util module.
Notes:
1) fsdb stands for file system database
2) all commands take input from a named file and produces output on standard out
3) If someone wants to make these commands more generally useful, they could change them to take piped input and produce piped output with different options for piping.  Then you could introduce a simple language that would allow you to string several commands together in order to process a file (gee, this really is starting to sound like a database)




Output:
Matlab can output files to multiple formats.  The most convenient are eps and jpeg
For example, adding these two lines to a matlab script prints the current figure in both jpg and eps
   print -djpeg90 -r0 /net/hp71/students/buttler/www/WebCQ/groupThroughput.jpg
   print -deps -r0 /net/hp71/students/buttler/WebCQ/paper/groupThroughput.eps
  

If you prefer color eps then you would use the line
   print -depsc -r0 /net/hp71/students/buttler/WebCQ/paper/groupThroughput.eps
   

Note that color graphs display poorly on black and white papers, so for conference submissions, I suggest that you only use the black and white version of eps output from matlab



Graph Types in Matlab

Matlab online documentation for creating graphs.

There are several types of plots that you can use from Matlab:
plot type Description
plot plot one or more lines on a graph
semilogx, semilogy use a log scale for either the x or y axis
loglog draw a line graph using a log scale for both the y and x axis
bar, bar3, barh, bar3h draw a bar graph

Monitor this page for changes