Sunday, January 31, 2010

switch, otherwise, disp

more things i should've known and/or always forget:

switch is supposed to be better than a list of if, elseifs. i can think of some cases where i should've used this.

a very clear and not flashy 3 minute video explaining this:
http://blogs.mathworks.com/videos/2008/01/02/matlab-basics-switch-case-vs-if-elseif/

otherwise is the default value of switch (also explained in video)

the video shows the use of disp, which is a very simple command to display text that i have forgotten in the past and spent a long time trying to remember. it is particularly good if you want to print things out to the screen to know how your program is running (e.g. error messages, transient values, loop number, etc.).

disp('SIGH');

Saturday, January 30, 2010

text as TeX or un-TeX

continuing the tradition of writing about stuff that i haven't really implemented in the past...

text properties

important: the default is to interpret strings as TEX instructions, and that is why the underscores in my titles always mess it up by creating subscripts. i need to set the text interpreter to none. then i need to be cool and start having some LaTeX equations in my other plots.

from the Text Properties page:

Interpreter
latex | {tex} | none

Interpret TEX instructions. This property controls whether MATLAB interprets certain characters in the String property as TEX instructions (default) or displays all characters literally. The options are:

latex — Supports a basic subset of the LATEX markup language.

tex — Supports a subset of plain TEX markup language. See the String property for a list of supported TEX instructions.

none — Displays literal characters.

Latex Interpreter

To enable the LaTEX interpreter for text objects, set the Interpreter property to latex. For example, the following statement displays an equation in a figure at the point [.5 .5], and enlarges the font to 16 points.

text('Interpreter','latex',...
'String','$$\int_0^x\!\int_y dF(u,v)$$',...
'Position',[.5 .5],...
'FontSize',16)



Wednesday, January 27, 2010

setting defaults: startup.m

this might have been useful to learn 7 years ago: make a startup.m file and put it in your path, you can set all sorts of defaults like line widths, colors, etc.

http://www.mathworks.com/support/tech-notes/1200/1205.html

see:
Section 5: Setting default property values

If I ever get around to it I'll set font to Arial, size to 14, LineWidth to 2. Or something like that.

Tuesday, January 26, 2010

octave: matlab's cousin

since we all like free things, and open source, here is the link to matlab's cousin, octave:

http://www.gnu.org/software/octave/

i've never used it, but i'd feel like a stud if i knew how.

GNU Octave is a high-level language, primarily intended for numerical computations. It provides a convenient command line interface for solving linear and nonlinear problems numerically, and for performing other numerical experiments using a language that is mostly compatible with Matlab. It may also be used as a batch-oriented language.

...There were still some people who said that we should just be using Fortran instead, because it is the computer language of engineering [if you were born in the 1800s], but every time we had tried that, the students spent far too much time trying to figure out why their Fortran code failed and not enough time learning about chemical engineering. We believed that with an interactive environment like Octave, most students would be able to pick up the basics quickly, and begin using it confidently in just a few hours.

***

Somebody needs to kill FORTRAN, okay? I guess if I actually knew that language, I could help the world be free of it. But I don't, I just like to make disparaging remarks about it. In ~10 years I'm going to be one of those old folks who still tries to hold on to the technology/software/languarges that I learned in my youth. I already feel sorry for myself now.

Monday, January 25, 2010

anti-illustrator

adobe illustrator has its purposes, but let those purposes NOT be to tweak matlab output so that it looks more aesthetically pleasing.  matlab output edited by illustrator one time is tolerable, but if you have to do it two, three, four times before you get your final plot, you're likely to poke your eyes out with a blunt object.

don't be a fool!

not that i've mastered these commands yet, but setting font, font size, line width, and tick properties goes a LONG way. i haven't used illustrator to "fix" a plot since i've started using stuff like this:

xlb=xlabel('strength (kPa)','FontName','arial');
ylb=ylabel('erosion','FontName','arial');

set([xlb ylb],'FontSize',14,'FontName','arial','LineWidth',1.5 )

axis square;

set(gca,'FontSize',14,'FontName','arial', 'LineWidth',1.5, 'Ticklength', [0.025 0.025])

gca is important.

Friday, January 22, 2010

axis of evil

i love me some square axes:

axis square;

i am also always setting axes bounds:

axis([xmin xmax ymin ymax]);

but there are tons of other axis options i have not checked out fully.

axis 'auto yz';

axis auto
axis manual
axis tight
axis fill
axis ij
axis xy
axis equal
axis image 
axis square
axis vis3d
axis normal
axis off
axis on


aesthetics people, aesthetics...

and another reason that matlab beats the pants off of excel.

Thursday, January 21, 2010

colors

colors are important if you want your MATLAB plots to be aesthetically pleasing. which you should. because it makes life more enjoyable. i mean, see how the colors of the header image and the titles and links sort of match each other on this page, that is NOT by accident. and it pleases me.

another advantage of specifying your own colors is that you can make a bunch of personal colors that convert to the correct gray-shade. that way you can make one set of images for presentations (colorful!) and easily convert it to grey shade for papers. ingenious!

here's a webpage with the RGB codes for many colors:
http://en.wikipedia.org/wiki/Web_colors

in matlab colors are specified with RGB values between 0 and 1, not 0 and 255. so simply specify the code given on that page, e.g. i,j,k, like so: [i/255 j/255 k/255]

example:
plot(.6,1.5,'o', 'MarkerEdgeColor', [0 0 0], 'MarkerFaceColor', [70/255 130/255 180/255], 'MarkerSize',11, 'LineWidth', 1.5);

steel blue, mmmmmm.

btw, "coulours" is one of my favorite songs by The Prodigy.

Wednesday, January 20, 2010

continue

this command reminds me of jay-z's song on to the next one.

continue: passes control to the next iteration of a for or while loop.

if [something that makes you want to skip the rest of the iteration go to the next one]
continue;
end

Tuesday, January 19, 2010

position and PaperPositionMode

There are lots of figure properties, including one i use to specify the size of screen figure output and also .jpg or .eps size. this is helpful if you care about the aspect ratio of your output, which you should.

figurehandle = figure('position', [x1 y1 x2 y2],  'PaperPositionMode', 'auto');

i usually start from the bottom left corner of the screen (x1 = 0, y1 = 0) with units in pixels. The 'auto' makes the printed output (.jpg, .eps, etc.) the same aspect ratio/size.

h1 = figure('position', [0 0 800 1100], 'PaperPositionMode', 'auto');

many other options! figure properties page.

Monday, January 18, 2010

datestr(now) is now

a very simple command to make a timestamp:

>> datestr(now)
ans =
18-Jan-2010 15:31:58

i like to write this string to my output files, especially if it is a file that i append to. then if you change something in the program on some date, you can look at lines before that date to see the difference. (or, if you make 11 versions of the same dumb program, you can always look at the output file and know which version it's from.)

i use these lines a lot to make the first 3 columns of output files. i guess it's overkill to output the date string for every single row/iteration, but whatever.

today = datestr(now);

fprintf(fid, '%s , %s, %d, ', today, filename, i);

the first line goes at the top of my program, and the second one is inside a loop for i.

Sunday, January 17, 2010

fit.m, an alternative to excel

it's the year 2010 and i finally figured out the fit.m function. why is this awesome? because it takes the place of the tedious excel fits i've used for over a decade. how many hours of my life have i spent clicking the chart button, scatter plot button, fit trendline, power fit, show equation, show R^2? very inefficient! so here is a way that i like much better*:

x and y are your vectors holding your x_i and y_i

A simple linear fit (polynomial order 1):
[cfun, gof, output] = fit(x,y,'poly1')

Finding the exponent of a power law fit:
xlog = log10(x);
ylog = log10(y);
[cfun, gof, output] = fit(xlog,ylog,'poly1')

cfun holds the fit parameters cfun(x) = p1*x + p2, which you can access by cfun.p1 or cfun.p2
gof holds goodness of fit parameters including rsquare and rmse (root mean square error)
output holds other parameters like numobs (# of observations), residuals, and more

i left off the ending semi-colons so the structures will appear automatically

or, you can output these to a text file like so:

fid = fopen('C:\path\filename.txt', 'a+');
 % open file for writing, append mode (will create file if it does not exist yet)

fprintf(fid, '%8.3f, %8.3f, %8.3f %8.3e, %d\n', cfun.p1, cfun.p2, gof.rsquare, gof.rmse, output.numobs); 
% the different formats used are f = fixed-point 8 digits, 3 decimals, e = exponential notation, d = integer, \n = newline

status = fclose('all');
% close the file properly so you can open it with other programs

this is especially useful when employed in a loop, say, if you want to look at a large data set and then certain subsets of that data, or if you want to look at y versus several different x variables.

here is a list of other fit types.
note/question: with the method above, i get the same power law fit parameters as using excel. there is a power fit option in matlab (instead of taking log10 and doing a polynomial fit) but i don't get the same answers.

now you can
(1) open the text file with wordpad (or other) to inspect
(2) use the file as input for matlab plot
(3) *sigh* open the file in excel as a comma delimited, save to .xls and plot

post questions or improvements to comments.

* i must admit that i probably wasn't using excel to its full capabilities by doing things the tedious way, but i just never learned any cool shortcuts in excel. that says something about me and something about excel.