Wait until .exe called with system() finishes execution
27 views (last 30 days)
Show older comments
Matlab skript saveMatfile.m turned in to .exe with Application Compiler.
I still want to use the .exe in matlab so i call it with system().
After i called the exe i want to open the saved .mat-file.
I get an error because the matlab script executes the load command before the .exe-file is done saving the .mat-file.
system(['start "" "saveMatfile.exe"', ' path_to_mat_file\mat_file.mat'])
load('path_to_mat_file\mat_file.mat');
I usually dont have this problem because other .exe finish before the exectution of the matlab code continues.
I also would like to have the text from the disp() commands from the saveMatfile.exe in the Command Window from matlab. Altough i do get it if i let it open the cmd.
But i get no response at all to matlab from the .exe.
Thank you for your help!
0 Comments
Answers (1)
Tejas
on 23 Feb 2024
Hello Christian,
I understand that the issue you are facing is that the ‘load’ statement is being executed before the completion of ‘saveMatFile.exe’. Since ‘saveMatFile.exe’ is responsible for creating ‘mat_file.mat’, the premature execution of ‘load’ results in an error because the file does not yet exist. Furthermore, you wish to ensure that all output from the ‘disp’ command in ‘saveMatFile.exe’ is displayed in the MATLAB command line window.
When the ‘system’ function is invoked with the ‘start’ command, it launches the specified executable in a separate environment. This means MATLAB will not wait for ‘saveMatfile.exe’ to complete before proceeding to the next command. Consequently, the ‘load’ command is executed before ‘saveMatfile.exe’ has finished running.
When the ‘start’ command is omitted from the ‘system’ call, the ‘system’ function directly requests the operating system to execute the given command. In this scenario, MATLAB pauses and waits for the command to execute completely before it returns the exit status to the ‘status’ variable. Since the operating system oversees the execution, it also ensures that the output from the ‘disp’ command within ‘saveMatfile.exe’ is displayed in the MATLAB command line.
Here is example code that demonstrates the effectiveness of this solution:
saveMatfile.exe
function saveMatfile(path)
a=4;
b=8;
save(path);
disp('This text is from saveMatfile.exe');
end
Solution.m
status =1 ;
status = system(['saveMatfile.exe mat_file.mat']);
if status == 0
disp('Data is loaded after exectution of saveMatfile.exe ');
load('mat_file.mat');
else
disp('Data is loaded before exectution of saveMatfile.exe ');
load('mat_file.mat');
end
Additionally, here are screenshots showing the desired results:
For more detailed information about ‘system’ function, refer to this documentation :
Hope it helps!
0 Comments
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!