Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Run MATLAB from command line

1,271 views
Skip to first unread message

Noam

unread,
Jul 4, 2007, 4:06:44 PM7/4/07
to
Hi all,

I would like to use MATLAB from the windows command line. I need some
scripts to run in the background but I couldn't really find a way how
to do this. I also look in the site for some clues on how to do it
but till now I couldn't find anything about it. I am sure it is out
there - some list of commands or some tips about it.

Do you know a link or how this is being done??

Thank
Noam

NZTideMan

unread,
Jul 4, 2007, 5:18:19 PM7/4/07
to

Did you try typing:
matlab -h
from a DOS window?
In brief, you can run your .m file, say mymfile.m like this:
matlab -r mymfile
providing it is in the Matlab path.

Noam

unread,
Jul 4, 2007, 5:58:05 PM7/4/07
to
Hi NZTideMan,

Many thanks for the tip.
I just gave it a try but it simply opens the MATLAB GUI as usual.
I also tried matlab -nodesktop from the command line (cmd or DOS).
What I get is a MATLAB console without a graphical GUI but this is
not exactly what I am looking for.

My aim is to control MATLAB with the help of the programming language
PHP. If I could give MATLAB commands over the DOS primpt line I could
do what ever I wanted with it and automate MATLAB.

I have MATLAB 7.0

Thanks
Noam

NZTideMan

unread,
Jul 4, 2007, 6:11:52 PM7/4/07
to
> > providing it is in the Matlab path.- Hide quoted text -
>
> - Show quoted text -

I assume you're using Win XP???
You need to go to Programs\Accessories\Command Prompt
A black window should open.
Type:
matlab -h

Believe me, it works.
I have 25 routines operating on an hourly schedule using the matlab -r
facility.
The only thing you need to be aware of is that you need an exit at the
bottom of each .m file, otherwise a Matlab will open each time and
stay open. I found the Matlab r2006b did not do this properly and one
morning I ended up with about 400 Matlabs open. I fixed this by
reverting to Version 2006a.


Noam

unread,
Jul 5, 2007, 1:41:26 PM7/5/07
to

Noam

unread,
Jul 5, 2007, 5:55:36 PM7/5/07
to
Hi NZTideMan,

Sorry for the last post - for some reason I got it empty...

That was exactly what I did yesterday :-(

I do use WinXP as you guessed. When I open the command line and type
matlab -h, MATLAB simply starts with the normal GUI as if I started
it by double-clicking on the matlab.exe file.

Pay attention that I didn't try to run any MATLAB script with the
command you gave me. I was hoping I will get a help list from the
command line that will give me instructions how to control a script
from the command line.

I am intending on delivering an array to a MATLAB script, processing
this array and returning some values.

I would like to do this with the help of PHP that is able to call
applications via command line and for that reason I can't use any GUI
functionality. Is that doable?

Anyhow - I don't manage to run MATLAB from the command line by using
the command you gave me.

Any further hints you could throw at my direction? I really
appreciate your efforts!

Thanks
Noam

NZTideMan wrote:
>
>
> On Jul 5, 9:58 am, Noam <behemothk...@hotmail.com> wrote:
>> Hi NZTideMan,
>>
>> Many thanks for the tip.
>> I just gave it a try but it simply opens the MATLAB GUI as
usual.
>> I also tried matlab -nodesktop from the command line (cmd or
> DOS).
>> What I get is a MATLAB console without a graphical GUI but this
> is
>> not exactly what I am looking for.
>>
>> My aim is to control MATLAB with the help of the programming
> language
>> PHP. If I could give MATLAB commands over the DOS primpt line I
> could
>> do what ever I wanted with it and automate MATLAB.
>>
>> I have MATLAB 7.0
>>
>> Thanks
>> Noam
>>
>>
>>
>>
>>
>> NZTideMan wrote:
>>
>> > On Jul 5, 8:06 am, Noam <behemothk...@hotmail.com>
wrote:

NZTideMan

unread,
Jul 5, 2007, 7:08:11 PM7/5/07
to
> > reverting to Version 2006a.- Hide quoted text -

>
> - Show quoted text -

I don't understand.........
Did you use Start\Run?
or did you do what I suggested and go Start\Programs\Accessories
\Command Prompt?
When you do this, does a black window open? It should be called
Command Prompt, and there should be line with a blinking cursor.
Something like this:
c:\>_
What happens when you type matlab -h into that black window?

Not that it matters, actually, because when you use matalb -r mfile it
opens a Matlab window and executes the .m file, but as I mentioned the
window will stay open unless you put an exit at the bottom of your .m
file.

Sid H

unread,
Mar 13, 2009, 10:27:01 PM3/13/09
to
Noam <behemo...@hotmail.com> wrote in message <ef5c7...@webcrossing.raydaftYaTP>...

I am not sure how far the command-line capabilities of matlab extend, but matlab CAN be definitely run in the batch mode. To get that to work, you should work in the xx\Matlab_installation_directory\bin\ directory. Then if you use the matlab -h command, it pulls up the batch file help info. (You need to be located in the directory with the batch file[matlab.bat] in there).

The reason why you get the matlab Java window (the desktop, with all the bells and horns) pop up when you type matlab -h now, is that 'matlab' is being recognized by the cmd interface to be the matlab executable, matlab.exe. I expect the other person, NZ.. already set his environment variables up to have 'matlab' refer to the .bat file instead of .exe.

So anyway, if you want to execute a wastedeffort.m file that you wrote up for batch processing, then you say: xx\matlab_installation_directory\bin> matlab -r wastedeffort.m. If you don't want any pesky matlab java window, then you say: xx\matlab_installation_directory\bin> matlab -automation -r wastedeffort.m.

I expect that you want to FORCE the windows CLI to behave like the matlab CLI, where you can declare variables/functions and have arithmetic I/O etc. I havent found a way of doing that so far without writing separate batch files so far. Another way is to (like Timo Salmi's tsfunc17) use C/C++ or use mex-stuff (I have no clue about mex) to create an original executable that carries out the i/o.

But that solution might be worse than the problem, if you don't like that kind of a thing.

Word of caution, Timo wrote tsfunc17 in 98.
-Take care
Sid

Sid H

unread,
Mar 13, 2009, 10:29:01 PM3/13/09
to
Noam <behemo...@hotmail.com> wrote in message <ef5c7...@webcrossing.raydaftYaTP>...

I am not sure how far the command-line capabilities of matlab extend, but matlab CAN be definitely run in the batch mode. To get that to work, you should work in the xx\Matlab_installation_directory\bin\ directory. Then if you use the matlab -h command, it pulls up the batch file help info. (You need to be located in the directory with the batch file[matlab.bat] in there).

Vinod Karanth

unread,
Mar 22, 2009, 10:24:01 PM3/22/09
to
Thanks for all your inputs. Inline with the above, I am planning to utilise about 40 computers to run my analyses remotely. I wish to run Matlab in the background without hampering the desktop user. I know that if I need to run an mfile on DOS prompt I should do this:
P:\Matlab2007a\bin>matlab /automation \r mfile
But this command makes the matlab command window to minimize but not put it behind the screen. I do not want the desktop user to note that Matab in running. I mean I want the matlab command window to disapper while running the matlab script. Is this possible to do in Windows? Please suggest.

Thanks either way.

"Sid H" <ssh...@xx.com> wrote in message <gpf4pd$e1d$1...@fred.mathworks.com>...

Yair Altman

unread,
Mar 23, 2009, 2:27:03 AM3/23/09
to
> P:\Matlab2007a\bin>matlab /automation \r mfile
> But this command makes the matlab command window to minimize but not put it behind the screen. I do not want the desktop user to note that Matab in running. I mean I want the matlab command window to disapper while running the matlab script. Is this possible to do in Windows? Please suggest.

Here is an unsupported and undocumented code snippet to programmatically disappear the desktop, editor and all the other windows that might pop-up when you open Matlab (depending on your default docking state):

frames = java.awt.Frame.getFrames;
for frameIdx = 1 : length(frames)
try
awtinvoke(frames(frameIdx),'setVisible',0);
catch
% never mind...
end
end

If you only wish to hide the desktop, then you don't need a loop - running awtinvoke on frames(1) will be enough. To unhide the windows, simply rerun the above with awtinvoke(...,...,1). Remember to place 'exit' in your m-code or you won't be able to close Matlab after the script finishes except using the task manager.

Yair Altman
http://Undocumented-Matlab.com

Vinod Karanth

unread,
Mar 23, 2009, 9:46:01 PM3/23/09
to
That was amazing. Thanks a lot for that Yair.

I have another question in mind--is there a way to do similar task (hiding the screen) for any window that has opened apart from matlab window? I have been seeking help on this for the last few days, but I can see only those using "autohotkeys" where-in you need to manually do it in person using the keyboard to hide or unhide an opened window. Could you please throw a code that can do similar job for an opened window (other than matlab). If we can name the opened window that has to be hidden/unhidden that would be just awesome. Thanks once again for your help.

Vinod

"Yair Altman" <altma...@gmailDEL.comDEL> wrote in message <gq7a3n$qus$1...@fred.mathworks.com>...

Yair Altman

unread,
Mar 24, 2009, 4:40:04 AM3/24/09
to
> I have another question in mind--is there a way to do similar task (hiding the screen) for any window that has opened apart from matlab window?

Come on!
set(hFig,'Visible','off')

Only hiding desktop-related windows is undocumented - hiding normal windows is fully supported

Yair Altman
http://undocumented-matlab.com

Vinod Karanth

unread,
Mar 24, 2009, 5:52:01 AM3/24/09
to
Thanks again Yair. I am still learning Matlab and always happy to see new things. Sometimes I wonder why I got into construction field and why not into software. Anywaz, I am running a software remotely, so this always produces a dos window (called a console window?). I needed to get that hidden....Thanks once again.

"Yair Altman" <altma...@gmailDEL.comDEL> wrote in message <gqa694$e7m$1...@fred.mathworks.com>...

Irina

unread,
May 18, 2011, 9:10:05 AM5/18/11
to
Hello,

as an extention of this topic I have a question. I can run matlab from batch file and it works. But I need myfile.m to return me a value (return 0 if everything is ok; return ErrorCode if somethings is not OK). If I try the following code:

matlab -nosplash -nodesktop -minimize -r "myfile(arg1, arg2,...)" -wait
echo %ERRORLEVEL%

then I always obtain ERRORLEVEL = 0 in any case (both if OK and not OK). Can I somehow control than myfile works properly by using ErrorCode in external application? I need to send ErrorCode from matlab to ... any external application. How I can do it? In definition of myfile.m there is a string:
function [Status] = myfile(arg1, arg2, ...)
but it doesn't work as I need to.

Thank in advance

Monica

unread,
May 28, 2012, 11:03:05 AM5/28/12
to
In this link you can download a library that allows you to connect from java to matlab, you can send and execute any commands you want. you can also download tutorial and videos on how to use it. As well as the source code.

http://code.google.com/p/matlabcontrol/downloads/list

Noam <behemo...@hotmail.com> wrote in message <ef5c...@webcrossing.raydaftYaTP>...
0 new messages