System Commands in Matlab

Another interesting feature that Matlab supports is to allow the usage of operating system commands either by using functions or a character identifier. We shall divide this into 3 ways for ease of understanding. First the OS independent way, then for the Unix based commands and finally for Windows.

The simplest way is to use the ! before the command. For example try: !ping http://www.google.com at your Matlab prompt. You will see exactly the same things that appear on the terminal / command prompt. Though, the major disadvantage with this type of input is the inability to use Matlab to the fullest. You cannot use or substitute values of variables in such commands and thats a big problem. This is solved by using the OS specific commands which are given below.

In Matlab for UNIX one can use all the shell commands that are generally available in the Linux / Unix terminal. The command is also called unix(‘command’). So the point I was trying to make above when I talked about variables in commands is something like:


path = '/home/makarand/';
pdfiles = dir([path,'*.pdf']);
unix(strcat('evince "',path,pdfiles(1).name,'" &'));

So basically, I am concatenating the string of variables (path) and structures (pdfiles.name) and constant text and feeding it to the Matlab prompt to evaluate by using the unix command. evince here is a document reader and the & is appended to push the process to back. This can be used to open any PDF file or used in a loop to open file after file in that directory.

Matlab can also run PERL scripts by using the perl(‘perlfile’) command.

Now coming to Windows, we have something similar to the above. But since the DOS interface is not that comprehensive (rather not well known as compared to UNIX shell commands) the dos(‘command’) is hardly used. Instead I would suggest using winopen(‘filename’) which runs that file if an executable, opens the document in MS Word if a .doc, a text file in notepad and so on.

All this seems to have some promise in making stuff happen automatically such that you can just code a little, and then sit back and enjoy the tinkering your computer carries on. 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s