Close matlab in shell script but keep the figure window.

7 visualizaciones (últimos 30 días)
dipak sanap
dipak sanap el 27 de Nov. de 2015
Comentada: Aurik el 7 de Sept. de 2023
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"

Respuestas (2)

Walter Roberson
Walter Roberson el 27 de Nov. de 2015
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.
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 comentarios
dipak sanap
dipak sanap el 28 de Nov. de 2015
Hey thanks for a reply. I realized, this approach is quiet complicated. So I am saving the data for each iteration in data file. I have files like data1, data2 and son on and I have another file lets say res_data. Now what I want to do is, intersect first two columns of every data file with res data and plot them as red on scatter plot otherwise plot them blue. I have written a script but it is not giving the desired out put. Could you please take a look at this script?
numfiles = 500;
q = fopen('res_data','r');
L = fscanf(q,'%d %d',[2 inf]);
L = L';
for i = 1:numfiles
f = fopen(sprintf('data%d', i), 'r');
D = fscanf(f,'%d %d %f %f',[4 inf]);
D = D';
P(i).element = D;
scatter(D(:,3),D(:,4),'b');
hold on;
fclose(f);
end
for j = 1:numfiles
[M(j).ele, im(j).ele,in(j).ele]=intersect(P(i).element(:,1:2),L,'rows');
amide = [];
for k = im(j).ele
amide =[amide, P(i).element(im(j).ele,:)];
end
amide_sd = amide(:,4);
amide_mean = amide(:,3);
scatter(amide_mean,amide_sd,'r');
end
hold off
Aurik
Aurik el 7 de Sept. de 2023
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

Iniciar sesión para comentar.


Aurik
Aurik el 7 de Sept. de 2023
Editada: Aurik el 7 de Sept. de 2023
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'];

Categorías

Más información sobre Programming en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by