Friday, January 16, 2009

MATLAB: Set working path at startup

Many times when MATLAB is started there is a certain program that you want to work on. In order to work on this program, the current directory or path of that program must be changed or set.  This little script here will prompt the user for the module to work on and then add that directory to the path listing. When MATLAB is terminated, that path will not be saved.
% startup.m

% Set Production Directory
prod='~/MATLAB/Production/';

% Prompt user for module name
module=input('What module do you want to work on? ','s');

% If module is set, add to top of path. Otherwise don't add
if ~isempty(module)
path([prod,module],path);
end
Place the startup.m in your MATLAB work directory.

Thursday, January 15, 2009

Plotting in MATLAB / Making Plots Look Pretty

Alright, well, here's the scoop. Ditto wanted a v2.0 remake of a post that we started late last year. And, well, I give him whatever he wants. So, without further ado, here's Pretty Plots in Matlab, v2.0.

Using MATLAB to crank through some calculations, but having trouble making the plots pretty? Here's an example that should make a nice looking plot that's ready to be inserted into a report.

X=[1 2 3 4 5 6 7 8 9 10];
Y=X.^2;

xmin=0;
xmax=11;
ymin=0;
ymax=110;

figure
set(gca,'fontsize',14)
set(gcf, 'PaperSize', [8. 6.],'PaperPositionMode', 'auto');
plot(X,Y,'k^','markersize',10,'markerfacecolor','r')
legend('mylegend','location','northeast');
xlabel('my $x$ label','Interpreter','latex','fontsize',16);
ylabel('my $y$ label','Interpreter','latex','fontsize',16)
AXIS([xmin xmax ymin ymax])
print('-r600','-dpdf',['filename','.pdf'])

Copy, paste, and run, and you should get this (in the directory your current directory):



Alright, a quick rundown.

1. The 'figure' command is letting MATLAB know that we're making a picture with all the following attributes.
2. The 'set gca' command is setting the axes, which have the default handle 'gca', to have a font size of 14 here. This will control the size of the numbering on the axes, as well as the size of the font in the legend.
3. The 'set gcf' command is setting the paper size to 8" by 6", a size I find works well with the default figure output of MATLAB. This will prevent you from having to crop the plot later in a third-party photo editor.
4. The 'plot' command lists the X and Y arrays youd like to plot, 'k^' plots black upward facing triangles in a 'marker size' of 10pt, and filled red.
The 'legend' command puts in your legend, one entry per pair of vectors being plotted, in the northwest position.
5. The 'xlabel' and 'ylabel' commands are pretty straightforward, but the interpretter command displays the labels in the default LaTeX font instead of the MATLAB font. You can use the '$$' pairing to put in maths (the x and y).
6. The 'axis' command sets the range for x and y, just comment this out if you like what MATLAB does.
7. The 'print' command will save your figure as .pdf with the filename you specify in the current directory (where your .m file is). Here, you can always specficy a path if you'd like to save figures in a different folder.

Still to come, using str2num and num2str to automatically number plots that are part of a for loop, or to name output graphs based on input data.

Friday, January 9, 2009

Woah, October 10, 2010

Today I had a mind blowing realisation that any geek would appreciate. Not sure if its well known but it soon will be. The number 42, which in case you didn't know, is the answer to everything, the universe and life, referring to The Hitchhiker's Guide to the Galaxy (see here).

Next year, October 10, 2010 will be here and it can be written as 10/10/10. Or removing the slashes, 101010 which converting from binary to decimal form is 42. Woah. Yeah, woah. Will the answer to life appear on October 10, 2010. I dont know, but someone will make mad money making t-shirts.

I think the meaning will come from the web comic xkcd.com. In case you did not know, the ordinal values of xkcd add up to 42.

Get your tinfoil hat ready.

Sarah Palin Pwns the Media Elite

Booyah!

enjoy...

-migs

Wednesday, January 7, 2009

The Music of Four Loves Four - 2008

As the 2008 comes to a close, here are the top ten artists that the author's of Four Loves Four listened to.

Ditto's Top Ten
  1. Radiohead
  2. Coldplay
  3. Van Morrison
  4. Pearl Jam
  5. Death Cab for Cutie
  6. Kanye West
  7. Mat Kearney
  8. Counting Crows
  9. The Decemberists
  10. Incubus
Doodah's Top Ten
  1. Tokyo Police Club
  2. Say Anything
  3. Owen
  4. Sufjan Stevens
  5. Someone Still Loves You Boris Yeltsin
  6. Vampire Weekend
  7. Royksopp
  8. The Appleseed Cast
  9. Pompeii
  10. Nada Surf
Mig's Top Ten
  1. Radiohead
  2. Death Cab for Cutie
  3. Christopher O'Riley
  4. Dave Matthew Band 
  5. Broken Social Scene
  6. Snow Patrol
  7. Jack Johnson
  8. Feist
  9. Van Morrison
  10. The Decemberists
A look at the musical tastes of the people who drive this site.
Thanks to all who subscribe.

Monday, January 5, 2009

I'm Boycotting the Progressive Insurance Commercial Girl...

...and so should you. Flo is bothering me just like Joe Buck did in 2004. We all know what happened there. (To refresh your memory)

Just so I know what I'm working against, I did a little detective work. Her name is Stephanie Courtney and here is a list of films and works I will not be watching until Progressive takes my demands suriously and ends these commercials.

Join my Cause.

-migs out

btw, I will be entertaining myself at the Nova/Seton Hall game tomorrow night at the ROCK and then watching the premier of Scrubs on ABC.


Thursday, December 18, 2008

Pretty Plots in MATLAB

Using MATLAB to crank through some calculations, but having trouble making the plots pretty? Here's an example that should make a nice looking plot that's ready to be inserted into a report.

figure
set(gca,'fontsize',14)
set(gcf, 'PaperSize', [8. 6.],'PaperPositionMode', 'auto');
plot(X,Y,'k^','markersize',10,'markerfacecolor','auto')
legend('mylegend','location','northeast');
xlabel('myxlabel','Interpreter','latex','fontsize',16);
ylabel('myylabel','Interpreter','latex','fontsize',16)
AXIS([xmin xmax ymin ymax])
print('-r600','-dpdf',['filename','.pdf'])

Alright, a quick rundown.

1. The 'figure' command is letting MATLAB know that we're making a picture with all the following attributes.
2. The 'set gca' command is setting the axes, which have the default handle 'gca', to have a font size of 14 here. This will control the size of the numbering on the axes, as well as the size of the font in the legend.
3. The 'set gcf' command is setting the paper size to 8" by 6", a size I find works well with the default figure output of MATLAB. This will prevent you from having to crop the plot later in a third-party photo editor.
4. The 'plot' command lists the X and Y arrays youd like to plot, 'k^' plots black upward facing triangles in a 'marker size' of 10pt, and automatically filled.
The 'legend' command puts in your legend, one entry per pair of vectors being plotted, in the northwest position.
5. The 'xlabel' and 'ylabel' commands are pretty straightforward, but the interpretter command displays the labels in the default LaTeX font instead of the MATLAB font. You can use the '$$' pairing to put in maths.
6. The 'axis' command sets the range for x and y, just comment this out if you like what MATLAB does.
7. The 'print' command will save your figure as .pdf with the filename you specify in the current directory (where your .m file is). Here, you can always specficy a path if you'd like to save figures in a different folder.

Still to come, using str2num and num2str to automatically number plots that are part of a for loop, or to name output graphs based on input data.

Wednesday, November 19, 2008

MATLAB: Don't ', transpose(it)

Doing a little homework for ME 7040, I discovered that there is more to transposing a matrix than just rows becoming columns and vice versa. I was doing a simple stress, strain, displacement analysis of a beam under static load. The code snippet in question is when the elemental stiffness matrix needed to be calculated by:
k = int(int(B'*D*B,r,-0.5,0.5),s,-0.5,0.5)
B is a 3xn matrix of shape functions and D a 3x3 relationship of Modulus of Elasticity and Poisson's ratio. There is obviously more to this code, including the calculations of stress and strain and the writing of an output file.

Running the code using the prime ('), the code took 143.106 seconds to run. I was initally confused by how long it took because the matrices were not that large. I then heard about the function transpose() and decided to use that. In code form:
k = int(int(transpose(B)*D*B,r,-0.5,0.5),s,-0.5,0.5)
This took only 19.735 seconds to run. So digging a little further, the operator ', also checks for complex conjugates of the matrix as well.

In conclusion, if you know that you are not dealing with complex numbers, you may want to use the transpose function and not its operator.

Tip: To time the running time of your code, put tic on the top line and toc on the last line.

Tuesday, November 18, 2008

SCP + SSH: Copying files from Windows to Linux

At the university I attend, it is mostly all about Windows and there is no support for linux so many things have to be done by the user to get anything done. However, most linux users are used to trying this on your own. Here I will show you how to successfully transfer files from your linux box at home to a windows computer at work/school. Linux commands refer to Ubuntu. Please see your distro's guide on installing packages.

Software Required:
Windows: Cygwin
Linux: openssh

Configuring linux box:
To install openssh, in terminal type
sudo apt-get install openssh-server
If the linux box is behind a router, please open the port corresponding to ssh. Default is 22. For more information please visit portforward.com.

To test the installation of ssh type:
ssh localhost
If everything works, then that should be up for setting up the linux box.

Configuring Windows:
After downloading setup.exe from the link above, double click to run. The important part of the installation is "Selecting Packages" diaglog box.
Expand the Net tree by pressing the +. Scroll down and look for openssh and click on "Skip" to change it to "Install". You will notice a check box under column B. Click Next when finished and complete the installation.

Operating the Windows Box:
After installation, open cygwin and a terminal will appear. The syntax for scp is:
scp [options] [[user@]host1:]filename1 ... [[user@]host2:]filename2

To copy 'report.pdf' from your Fall 2008 folder on your home directory, you would type:
scp report.pdf user@serverip:"/home/user/Fall 2008/" .
This will save report.pfg into your home directory. Notice the "."

More Information:

Good Luck.

Saturday, October 11, 2008

Conky: Display Last Pidgin Message

Recently, I removed the gnome-panels, added awn, and had a conky bar at the top. However, I missed having a system notification of when I would receive a message via Pidgin. So I browsed the pidgin wiki and create somewhat of a hack to display the last IM that received from Pidgin. This method is probably not the best way however I had some diffuculty.

The DBus method that listens for the received messages was chocking conky and not allowing the conky script to load correctly. So I had to create two files. getim.py would run at startup, listen for a new message and then write that message to a data file. lastim.py would be executed within conky and check the data file for the new message. I know its not real efficient and redundant but it just works. If you have any brilliant ideas please let me know.

Here is the getim.py file:
#!/usr/bin/python

# ----- GETIM.PY // LISTEN FOR AN RECEIVED MESSAGE ------------#
################################################################
# MOST OF THIS SCRIPT WAS TAKEN FROM THE PIDGIN DEVELOPER WIKI#
# PLEASE VISIT http://developer.pidgin.im/ FOR MORE INFORMATION#
################################################################

def conky_im(account, sender, message, conversation, flags):
reg = '<(.|\n)+?>' # REGEX FOR HTML TAGS
message = re.sub(reg,'',message) # REMOVE HTML TAGS
sender = sender.split("@")[0] # GET GCHAT NAME
message = message[0:24] # LIMIT TO 25 CHARS
file = ' ' # LOCATION OF DATA FILE
fim = open(file,"w")
IM = sender + " said: " + message
fim.write(IM)
fim.close()

import dbus, gobject, re
from dbus.mainloop.glib import DBusGMainLoop
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SessionBus()

bus.add_signal_receiver(conky_im,
dbus_interface="im.pidgin.purple.PurpleInterface",
signal_name="ReceivedImMsg")

loop = gobject.MainLoop()
loop.run()



Copy and paste the following and save as lastim.py:
#!/usr/bin/python

# SHOW LAST IM
f=" " # REPLACE WITH YOUR DATA FILE. ie.CONKYIM.DAT
fim = open(f,'r')
im = fim.read()
print im
fim.close


Finally, add this to your .conkyrc script
${execi 1 python ~/scripts/lastim.py}

Any questions/comments let me know. Here is a screen shot of what my conky looks like: