I realize I have a short memory of computer related skills, so rather than always going back to my old code to refresh on syntax and tricks, I’d rather record them here.
The nastiest aspect is how to generate graphs and plots automatically, and elegantly, and save them expediently. I don’t want to reinvent the wheels every time, so let’s go through the work flow of creating a plot, and saving it:
if you have two vectors and , to plot against you simply use the command:
% t is the vector [1 2 3 .. length(x)] where length(x) is the dimension of the vector x.
Notice in matlab the most basic form of instantiating an array follows the form
v= [v1 v2 v3 .. vk]
separated by single-space.
More generally, you can plot two graphs on the same plane, as follows:
here ‘–‘ denotes that the second plot is given by dashed lines, as opposed to solid ones.
Now remember every time only one plot is displayed, in a plot window. To save it, use the following command:
savefig(‘matlab/convex optimization/originalsignal’, ‘pdf’);
Several things worth mentioning here: matlab functions are stored in external .m files. Here the savefig.m file can be downloaded from internet. The file name is split into two components: the first being the path and file name without extension. Notice that even for linux pathname, there is no need to use escape sequence for the space in the folder name “convex optimization”; usually one has to type “convex\ optimization” for bash to recognize.
If a more complicated file name is desired, follow the next command example:
num_picture = 5;
savefig([‘matlab/convex optimization/Qimg’ num2str(num_picture)], ‘pdf’);
Here we treat the path and file name together as a string, which is an array of characters. Since in matlab an array is described by [a b c ..], the above example would generate the file name
which is very convenient when you need to generate multiple graphs and save them automatically in one script run. Be careful to include the space between the root filename and the number. Also I believe for filenames it’s best to use integers, rather than floating numbers (the latter didn’t work for me). But for things like plot titles and axis labels, floating numbers can be included using the num2str function without a problem, as the next example shows
tradeoff = 0.15;
noise = 0.2;
TVres = some vector;
ylabel(‘TV reconstructed signal’);
title([‘var = ‘ num2str(noise) ‘ , tradeoff = ‘ num2str(tradeoff)]);
Here we first plot TVres against x, then we start putting x-axis label and y-axis label on it, and finally put a title at the top of the graph. Notice that the title is a string consisting of four pieces, as described before: two literal strings, and two strings converted from numbers noise and tradeoff. Notice here it’s ok to use floating numbers.
One trick that reduces workload is to do a for loop over a specific set of objects, as in
for i = [1 4.3 2]
So i can range over any array, with 1:k only a special case.
Finally to adjust fonts of texts within a plot, say you want to add labels to each point in a plot, then you can use
for j = 1:k
text(x(j),y(j), num2str(j^6/k^6 * maxtradeoff), ‘FontSize’, 12);
here the text function simply puts the third argument string num2str(..) at the position (x(j),y(j)) of the plot. The last two arguments set the font size. This was useful to me when I need to determine the tradeoff parameter corresponding to each point on a tradeoff curve, which is implicitly parametrized by the tradeoff parameter.
By the way, a really cool trick I learned by googling just now is this: if you hold shift down when pressing enter in wordpress post, you get to type stuff in single-spaced lines rather than double-spaced, which is the default behavior. This is nice when you want to include code snippets for example.