필터 지우기
필터 지우기

Try, Catch not working with system command.

조회 수: 5 (최근 30일)
Adam Hicks
Adam Hicks 2017년 11월 14일
편집: per isakson 2017년 11월 14일
In my program I have the following block of code,
try
!"C:\Program Files\MATLAB\R2016b\bin\matlab.exe" -r "foo"
catch
warning('Directory was not found. Please locate the MATLAB R2016b installation.');
[f2016, d2016] = uigetfile('*.exe','Locate MATLAB R2016b Executable');
cmd = strcat('!"',d2016,f2016,'" -r "foo"');
eval(cmd);
end
When I run it normally, everything works fine. But when I alter the path in try to create an error, the catch is never executed. Instead the program terminates and I'm left with "The system cannot find the path specified". This text isn't displayed as an error which is probably the issue, but is there any way to make my try, catch work? Or at least a workaround?

채택된 답변

Walter Roberson
Walter Roberson 2017년 11월 14일
I recommend converting to use system()
path_to_matlab = '"C:\Program Files\MATLAB\R2016b\bin\matlab.exe"';
try_again = true;
while try_again
try
cmd = sprintf('"%s" -r "foo"', path_to_matlab);
[status, result] = system(cmd);
if status == 0; try_again = false; end
catch ME
end
if try_again
warning('MATLAB was not found. Please locate the MATLAB R2016b installation.');
[f2016, d2016] = uigetfile('*.exe','Locate MATLAB R2016b Executable');
if isnumeric(f2016)
fprintf('Canceling');
break
end
path_to_matlab = fullfile(d2016, f2016);
end
end
It is not clear why you are starting a new MATLAB session ?
You should consider using
if ispc()
path_to_matlab = fullfile(matlabroot, 'bin', 'matlab.exe');
file_select_ext = '*.exe');
else
path_to_matlab = fullfile(matlabroot, 'bin', 'matlab');
file_select_ext = '*');
end
try_again = true;
while try_again
try
cmd = sprintf('"%s" -r "foo"', path_to_matlab);
[status, result] = system(cmd);
if status == 0; try_again = false; end
catch ME
end
if try_again
warning('MATLAB was not found. Please locate the MATLAB R2016b installation.');
[f2016, d2016] = uigetfile(file_select_ext, 'Locate MATLAB R2016b Executable');
if isnumeric(f2016)
fprintf('Canceling');
break
end
path_to_matlab = fullfile(d2016, f2016);
end
end
  댓글 수: 1
Adam Hicks
Adam Hicks 2017년 11월 14일
I'm running MATLAB R2010b due to functionality that hasn't been updated for newer versions yet. However, the final step of my program requires functionality that only exists in newer versions of MATLAB, hence why I am calling 'foo' in a new MATLAB instance.

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

추가 답변 (1개)

Fangjun Jiang
Fangjun Jiang 2017년 11월 14일
편집: per isakson 2017년 11월 14일
use system() to replace the "!" to call your program.
[status,result] = system('command')
Use the returned "status" to determine your next step.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by