Monday, February 3, 2014

How to insert MATLAB codes into a LATEX document

LATEX can be messy if you don't know what you're doing. Sometimes it's not easy to get used to if you're coming from a Microsoft Office background. It took me a while to figure out how to do different things like creating a table, adding multiple columns, drawing a square around an answer, etc. But once you figure it out, latex gives you remarkable results. Some of the things are not straightforward like in MS Word, but sometimes it's worth taking time to learn how to do things. There are so many resources out there on the internet. Which brings me to this post. I like to use LATEX to do my homework. Sometimes I have to use MATLAB to solve some numerical problems, or to graph something, etc. The professor expect me to submit the MATLAB code with the homework. I can just copy and paste the code from an M-file to the latex document. But it doesn't keep the formatting of the M-File.

To Solve this problem a guy named Florian Knorn at the MATLAB central created a style file which lets you do this very task. He calls it the MCODE. You can download the mcode by clicking this link which will direct you to MATLAB Central.

How to use MCODE:

  • To use mcode, first you have to download it and put it in the same directory as your .tex file which you're trying to compile. 
  • Then, include mcode as a package in your .tex file:
  • \usepackage[]{mcode}
    
  • Some options can be added between the square brackets:
    • bw : Black and White
    • numbered : Number the lines of your code
    • framed : Draw a frame around your code
  • Now to include MATLAB code one of two ways can be used.
  1. Copying and Pasting the MATLAB code:
  2. \begin{lstlisting}
    clear;
    clc;
    len=1000;
    L2=zeros(1,len);
    index=1;
    \end{lstlisting}
    
  3. Using the Path of the M-File:
  4. \lstinputlisting{/path/mfile_name.m}
    

Complete Example:


\documentclass[12pt]{article}
 
\usepackage[numbered, framed]{mcode}
 
\begin{document}

\title{MATLAB Code in LATEX}
\author{Saliya}
\date{} 
\maketitle

Copy and Paste Method:
\begin{lstlisting} 
clear;
clc;
len=1000;
L2=zeros(1,len);
index=1;
\end{lstlisting} 

Using the path of the M-File:
\lstinputlisting{mfile.m}

\end{document}

Result:


Saturday, February 9, 2013

getch in python

Few days ago I started learning Python for one of the classes I'm taking. In the very first Python program I wrote, after an answer was displayed I wanted to wait for a key input from user before the next line was executed. If I was programming using C or C++ this could have been done easily using getch() function. Therefore I searched for something equivalent in Python. After reading couple of threads I realized that Python does not have something similar to getch. May be I'm wrong, there is something similar to getch and I couldn't find it. However I was able to use a simple way to produce the same effect I was looking for using the Python's input command.

input("")

Since input command prompts a massage to the user and assigns the user input to a variable, using input("") without any argument and without any variable assignment, I was able to pause the program execution until user hit 'enter'. It's not quite the same as getch but it's close. If you are looking for a getch equivalent in Python this might be useful for you.

Scheduling a task in Linux using crontab

Recently, when I was working on my research I had to check whether a particular file was updated the way I wanted. One way of doing this was to sit by my computer and continuously check the file whenever it updated. This file was created from a continuous process that can run for days until was killed. Obviously I did not want to sit by my computer for days looking at that file. Fortunately there is another way to do this in Linux systems. Linux has a utility called 'cron' to do this task.
In my case, I created a small shell script to copy the file in question to a different location with a different file name. Every time I run that script, it creates a copy of the file I want to a different location with a new file name. Now I had to set up a way to run the script every few minutes automatically, so I can come back later and check the new set of files.
If you are looking for a way to do a similar task, you also can use cron utility in Linux.

To do this, first you have to open up the cron utility. Go to the terminal and type,

crontab -e

Now, to schedule a task you have to follow the format as follows,

MIN HOUR DOM MON DOW CMD

MIN    - Minutes, can have values from 0 - 59
HOUR - Hours, can have values from 0 - 24
DOM   - Day of the month, can have values from 1 - 31
MON   - Month, can have values from 1 - 12, 1 being January
DOW   - Day of the week, can have values from 0 - 6, 0 being Sunday
CMD   - The command you want to execute

So Let's say I want to execute a script in my 'Documents' directory every 30th minute, every hour, everyday. In that case I can use the following line in my cron

30 * * * * /home/usr/documents/script_name.s

asterisk(*)  is used to represent all the values in a particular field. Which means my "script_name.s" file will be executed every 30th minute of every hour, every day, every month.

Let's look at another example. Let's say I want to run my script every weekday at 6.00 p.m,

0 18 * * 1-5 /home/usr/documents/script_name.s

what if want to run my script every 5 minutes

*/5 * * * * /home/usr/documents/script_name.s

You are not limited to run scripts with cron. You can execute any command you want. For example, when I first found out about cron, I used the echo command to write a line into a file every minute just to check if I was doing it correctly.

*/1 * * * * echo "testing cron">>/home/usr/documents/cron_test.txt

These examples will give you an idea on different ways you can use cron utility to schedule your tasks. As you can see, you have lot of flexibility in scheduling tasks using cron. Once you type your cron command, just save the cron file and you're done.

You can schedule multiple tasks using cron and if you want to comment out any line from the cron file, you can use # symbol to do so.