try/catch with timeout

조회 수: 43 (최근 30일)
Lionel Pöffel
Lionel Pöffel 2021년 3월 18일
댓글: Lionel Pöffel 2024년 3월 5일
I have an instruction block with communication to external components, enclosed in try/catch. However, instead of throwing an exceptionand skipping to the "catch" part, the execution simply stalls under certain conditions. Is there any way top realize something like
try (max execution time x seconds)
instructions with possible stall
catch ME
will be executed either if instructions within try throw an exception or their execution takes more than x s
end
  댓글 수: 1
Rik
Rik 2021년 3월 18일
As far as I'm aware, this is only possible by spawning a new process, which then needs to signal to the main process that it is done by writing a file or something similar.

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

답변 (1개)

Harsh Mahalwar
Harsh Mahalwar 2024년 3월 5일
편집: Harsh Mahalwar 2024년 3월 5일
Hi Lionel,
As there are several parts to this question, I will be solving it iteratively.
  1. Running an external application/component from MATLAB.
This can be achieved by using the start command along with the “system” function in MATLAB.
2. Waiting x seconds for the application’s process to end.
This can be achieved by using timeout or PING commands along with “system” function in MATLAB.
Here's a workaround which uses "system" function of MATLAB along with start and ping commands in Windows to achieve the same:
line1 = convertCharsToStrings('start "" "C:\Users\harsh\Desktop\Demo.txt"');
line2 = "PING -n 6 127.0.0.1>nul";
line3 = convertCharsToStrings('taskkill /F /FI "WindowTitle eq Demo - Notepad" /T >> "logs.txt"');
try
% opens the demo.txt file
system(line1);
% timeout of 5 seconds
system(line2);
% tries to kill the process and creates logs.txt with it's output.
system(line3);
% Read the log.txt
output = readlines("logs.txt");
logOutput = convertStringsToChars(output(1, 1));
% If the first 7 characters are eq to 'Success' that would mean that
% our script had to terminate the process.
if logOutput(1:7) ~= 'SUCCESS'
disp("Process was closed on time!");
else
error("Process Terminated!");
end
catch exception
disp(['Error: ' exception.message]);
end
(Note that this workaround is valid for Windows only)
You can learn more about “system” function in MATLAB using the following documentation:
I hope this helps, thanks!
  댓글 수: 1
Lionel Pöffel
Lionel Pöffel 2024년 3월 5일
Thanks for providing the input.
My question was posed a long time ago, so we had to find another work-around in the meantime, which was to systematically avoid the root-cause for the stalling mentioned above.
If your answer would be applicable to our use case is not fully clear, because we used the MATLAB Excel connection, so the system command may not have worked.
But it is a good approach that can help in other circumstances.

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

카테고리

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