显示标签为“Matlab”的博文。显示所有博文
显示标签为“Matlab”的博文。显示所有博文

2013年3月31日星期日

Several Tips for Publishing Images from Matlab: PDF to Word, Font Size in Ubuntu etc.

Matlab provides some great convenience in creating high quality figures. However, esp. when you try to integrate these figures into your Word version of the paper, quite a few tweaks are needed.

1. Create, crop and insert high quality PDF images into word.

Matlab can create PDF or EPS figure quite easily by the print command, and you can even use LaTeX to combine multiple figures into one when you have to (subplot in Matlab create a very large margin between figures). However, one problem is to crop off the margins around the PDF. "pdfcrop" is a command line program in Ubuntu, but apparently it can not remove the margin on the bottom of the PDF. I found a nice Java program:

http://www.pdfscissors.com/

This can do the job quite nicely.

Then, before inserting PDF into word, keep in mind that there is no way to include such vector image into M$ Word documents. We have to convert it into high-quality TIFF image. There are quite a few options for this, just make sure to create TIFF image with 600 dpi. Just to be safe.

2. Change font size of Matlab figure in Ubuntu and save it into EPS.

This is a weird problem I had for a long time. Every time I tried to set the font size of Matlab figure in Ubuntu, it does not work. The same code works fine in Windows 7. The problem was that a font package is missing for Ubuntu. Just type:


sudo apt-get install xfonts-75dpi

Log out and log in, then it all works fine. Also, before save the figure as EPS, make sure to include:

set(gcf, 'PaperPositionMode', 'auto');

2012年6月17日星期日

How to use nice colors in Matlab Plot

One major problem I always had in Matlab plotting is that other than the "default" 6-7 colors, it's hard to use different colors.

I just found a really nice solution:
http://scslin.blogspot.com/2011/09/matlab-plot-with-more-colors.html

Basically, you first download a small function "rgb" from:
http://www.mathworks.com/matlabcentral/fileexchange/24497-rgb-triple-of-color-name-version-2

Then, you can call:
plot(x,y,'color',rgb('olive'));


You will be able to draw a line using a "olive" color.
The available colors are as follows:


2012年5月15日星期二

Some useful functions for subplot in Matlab

So there are two things that the subplot function in Matlab can not do.

1. Generate an "overall" title on the top of all sub-figures.
2. Make the sub-figures as compact as possible (usually very large margins will be made).

Normally, what we can do is save each sub-figure as eps or pdf, and use LaTeX to organize them. This works well and I highly recommend this way when you are preparing publication ready figures, but there are times that you just want to generate some report quickly and nicely.

So I found this two nice functions in Matlab file exchange:
1. mtit. This one will place a title automatically for you on top of everything.
2. subaxis. This one does roughly the same thing as "subplot", but with options that enables you to remove margins.

2012年5月9日星期三

Solving the LaTeX error: "Too many unprocessed floats"

Yesterday I generated a LaTeX document using Matlab script. This document have about 50 tables, with even table embedded with 30 images. So LaTeX thought that it I can't handle this much floating objects.

I searched around on the internet and found the best solution for me is to add
\clearpage
after every table. This command will force LaTeX to dump all the cached floating objects till here. So basically it will print one table per page, which is fine in my case.

2011年10月28日星期五

Multivariate T-test

For a long time I do not even know that this type of test even existed...

I always thought that in order to extend t-test to multivariate, we have to try to generalize 2 sample t-test, change the x_i - x_j to the 2-norm etc. It turns out that there is a more conventional way to do it...

It is called, Hotelling's T squared test.

And someone have a nice Matlab package for it:

2010年1月30日星期六

Common Data Sets for Machine Learning

Multi-Label:
http://mlkd.csd.auth.gr/multilabel.html

Comprehensive:
http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/

2009年11月6日星期五

Tips in Mex Fortran, linux version

Starting from the day before yesterday, I need to write a Matlab interface for a Fortran source code, which is almost a black box for me, so that I can use "mex" to compile it and call it in Matlab.

The first thing is the compiler. In Windows, you can only choose a specific compiler, say, C/C++ or Fortran. So if you work with both C and Fortran code, you will have to use "mex -setup" from time to time, which is kinda annoying for a lazy person like me. But in Linux, you can work on a configuration file so that Matlab will automatically select the compiler for you.

First, in "/opt/R2009a/unix/bin/mexopts.sh", you can see the following segment:

#
FC='g95'
FFLAGS='-fexceptions'
FFLAGS="$FFLAGS -fPIC -fno-omit-frame-pointer"
FLIBS="$RPATH $MLIBS -lm"
FOPTIMFLAGS='-O'
FDEBUGFLAGS='-g'
#

The first line indicates the compiler for Fortran. "g95" is not provided, so, we will change it to gfortran which I think comes with Ubuntu. Thus, change this segement into:

#
FC='gfortran'
FFLAGS='-fexceptions'
FFLAGS="$FFLAGS -fPIC -fno-omit-frame-pointer"
FLIBS="$RPATH $MLIBS -lm"
FOPTIMFLAGS='-O'
FDEBUGFLAGS='-g'
#

Then, in Matlab, if you type
mex -setup

You will see something like:
The options files available for mex are:

1: /opt/R2009a/unix/bin/gccopts.sh :
Template Options file for building gcc MEX-files

2: /opt/R2009a/unix/bin/mexopts.sh :
Template Options file for building MEX-files via the system ANSI compiler

Choose 2. Then it's ok to compile.

The next thing is the interface. The help in Matlab has everything you know to create a wrapper, even some examples for you to follow. So although I know nothing about Fortran, I managed to write a "almost" complete interface which passed the compilation.

However, there're several important tips that costs me a whole day to figure out.
  1. The name of the source file must be ".F", otherwise it won't compile.
  2. If the source code you want to wrap was not developed by you, be very careful about all the data types in that code. For example, if one input for that function is "REAL", then it is 4-byte floating number. And in Matlab, unless specified, everything will be "double", which is 8-byte floating number. If you pass the double value directly into it, the value will NOT be preserved. So when you call the function in Matlab, use something like "nlam = int32(nlam);

    beta = single(beta);" Also, inside the interface, use something like "mxCopyPtrToReal4(beta_pr,beta,size)".
  3. The next thing is managing the output. When you create the output matrices, do not use mxCreateDoubleMatrix. Use "mxCreateNumericMatrix", which will let you to specify the output types like int32 or single.

2009年8月22日星期六

Round up numbers in Matlab, leaving given numbers of digits after dot

The easiest way is to try to
num = num*10;
num = round(num);
num = num / 10;
kind of thing...

Other way is:
the vpa function.

2008年12月12日星期五

Matlab split filename

I don't know why it's so hard for me to finally find this function...

[pathstr, name, ext, versn] = fileparts(filename)

2008年10月4日星期六

Using the malab remotely

It is so disappointing when I found it is so slow to use the ssh -X to run Matlab in my office from my apt. Turns out it is the problem of jre...

Thus, I tried to do it without most of the GUI:
ssh -X
matlab -nodesktop -nosplash

then, it became a command line version of matlab. When I do things like edit x.m, the GUI editor still shows up! And pretty fast.

whos will give all the current variables in the worksapce. So the debugging will be fine.

helpwin give me the GUI help, good.

2008年10月3日星期五

Matlab Linux can not show command window

After installing Matlab r2007a on my TA computer, one weird problem occurred. It seems that the command window can not be displayed, only a blank window. Anything else seems to be fine, coz when I click start, I can run simulink and so on.

In the command line, the error is:

Locking assertion failure. Backtrace:
#0 /usr/lib/libxcb-xlib.so.0 [0xb558d767]
#1 /usr/lib/libxcb-xlib.so.0(xcb_xlib_unlock+0x31) [0xb558d8b1]
#2 /usr/lib/libX11.so.6(_XReply+0xfd) [0xb591e1bd]
#3 /usr/local/mathworks/sys/java/jre/glnx86/jre1.6.0/lib/i386/xawt/libmawt.so [0xaef5122e]
......

export MATLAB_JAVA=/usr/lib/jvm/java-6-sun-1.6.0.06/jre

then everything goes well again...

Just remember to modify the version of java when updated...