Hello,
I basically want to call MATLAB from STATA to create a file, then returning the control to STATA. I have this piece of code:
cd "${REGCON_read_path}"
shell matlab -noFigureWindows -r "try; run 'regcon_cnae_map.m'; catch; end; quit" -wait
So MATLAB opens a session in the correct folder, but it seems that it just opens the script and closes it without 'clicking on run'. I would appreciate if somebody could tell me what I am doing wrong.
I guess this is the correct forum since I could well call matlab from the windows prompt instead.
KR

 Accepted Answer

the cyclist
the cyclist on 3 Apr 2021

0 votes

I haven't run MATLAB from the command line in probably 20 years, but I think I would try putting the whole try-catch structure into its own *.m file, and call that. (I am not able to test that idea right now.)

2 Comments

Hi,
Thanks for you answer.
Although I don't understand what you mean by ' try-catch structure into its own *.m file', an example would be very helpful if posible.
Of course, this is the way I found in the Internet, if you knew a better way I would appreciate if you shared it.
KR
For debugging purposes, I would try to simplify what you are doing. So, I'd recommend the following.
First, try not calling from Stata. Just call MATLAB from the command line directly, to see if that works. That would look like
matlab -noFigureWindows -r "try; run 'regcon_cnae_map.m'; catch; end; quit" -wait
I think I would also remove the quit statement, so that the MATLAB window stays open when it is done. Maybe you'll get a clue from the open MATLAB window. That simplifies the command to
matlab -noFigureWindows -r "try; run 'regcon_cnae_map.m'; catch; end" -wait
I think I would also remove the try-catch block. The way you have written this, MATLAB is told, "Try to run regcon_cnae_map.m, but if there is an error, do nothing." ("Do nothing", because there is no command after the "catch".) That simplifies the command to
matlab -noFigureWindows -r "run 'regcon_cnae_map.m'" -wait
or, even simpler
matlab -noFigureWindows -r "regcon_cnae_map" -wait
That is a more barebones version, that simply opens MATLAB and runs the file regcon_cnae_map.m. (Notice that this last version of the command does not have the ".m" at the end.)
My guess is that your file is going to throw some error, which is why the try-catch structure opened it and then shut it down.

Sign in to comment.

More Answers (1)

Rubén Pérez Sanz
Rubén Pérez Sanz on 6 Apr 2021

0 votes

Many thanks, you approach worked perfectly.

Categories

Find more on Historical Contests in Help Center and File Exchange

Products

Release

R2020b

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!