Call matlab from STATA

조회 수: 36 (최근 30일)
Rubén Pérez Sanz
Rubén Pérez Sanz 2021년 4월 3일
답변: Rubén Pérez Sanz 2021년 4월 6일
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

채택된 답변

the cyclist
the cyclist 2021년 4월 3일
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
Rubén Pérez Sanz
Rubén Pérez Sanz 2021년 4월 4일
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
the cyclist
the cyclist 2021년 4월 5일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Rubén Pérez Sanz
Rubén Pérez Sanz 2021년 4월 6일
Many thanks, you approach worked perfectly.

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by