Close matlab in shell script but keep the figure window.
7 次查看(过去 30 天)
显示 更早的评论
My matlab script plots a scatter plot.I am running this matlab script within the shell script. My shell script has for loop. I want to close only matlab for every iteration of shell for loop but keep figure window on in order to plot scatter data for all iteration on same figure. Is there any way to enable figure window throughout the shell script run ?
matlab -nodesktop -r "matlabscript; {dont want to close figure window} ; exit"
0 个评论
回答(2 个)
Walter Roberson
2015-11-27
No, not in the way you are proceeding. However, if you are using one of the Bourne shell derivatives, including ksh or bash, then you can open a pipe to a process and read and write to the process.
One route is to use named pipes; see http://www.linuxjournal.com/content/using-named-pipes-fifos-bash
With ksh itself (but not bash) you can use the little-known facility known as "co-process"; see the ksh command page for "|&" and the <&p and >&p and the "-p" option of print.
2 个评论
Aurik
2023-9-7
There's two (potential) issues I see
In the second for loop, you should have P(j).element instead of P(i).element
You should also have an fclose(q) after the fourth line L = fscanf(q,'%d %d',[2 inf]);
I dont know anything about your files or plot so there may possibly be other issues
Aurik
2023-9-7
编辑:Aurik
2023-9-7
Unfortunately the only way I was able to solve this was by eliminating the "exit" portion of the command. In this case, the figure will not close, the command window will also remain open. In this case, you must close the command window manually (either clicking the 'X' or by typing 'exit'); doing so will also close the figure.
---
Another way I circumvent this issue is by saving the figure out to an image every time it is generated. This code must be included within the MATLAB script.
% Insert your figure here
fig = figure;
scatter(1:10, rand([1,10]))
% Name of the image file
image_filename = 'image.png';
% Full file path (with directory)
image_filepath = fullfile('path_to_output_folder', image_filename);
saveas(fig, image_filepath)
When running the same script multiple times, sometimes I generate a unique filename so that I dont always write over my previously-generated images.
% Unique image filename, using the date/time for uniqueness
image_filename = ['image', char(datetime('now', 'Format', 'yyyyMMdd_HHmmss')), '.png'];
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!