john.steak
john.steak

Reputation: 59

Closing specific figures does not work as I need - it just cleares them

I have a GUI in Matlab, where I can plot many figures, I have named them specifically, and now I'm currently working on a delete function, after clicking on a button connected to the function, I want the figures get closed (not only cleared, but closed), but it cleares them instead.

Code below:

if exist('Vectorcardiogram')
close('Vectorcardiogram')
return
end

if exist('Planes')
close('Planes')
return
end

if exist('P wave')
close('P wave')
return
end

if exist('QRS complex')
close('QRS complex')
return
end

if exist('T wave')
close('T wave')
return
end

As you can see, I can plot 5 figures in total, but I don't need to plot all of them always, so that's why I wrote the code that way it is.

Could you please help me, why is it clearing the specified figure windows instead of closing them?

Thank you!

Upvotes: 0

Views: 62

Answers (1)

Rotem
Rotem

Reputation: 32094

You need to get a handle to your figure first:

%Open figure named 'Vectorcardiogram'
figure('Name', 'Vectorcardiogram');

%Return handle to figure named 'Vectorcardiogram'.
h = findobj('Name', 'Vectorcardiogram');

%Close figure.
close(h);

More elegant solution is saving handle to figure, when open it, then use handle for closing the figure.

Example:

Vectorcardiogram_handle = figure('Name', 'Vectorcardiogram');
%...
%...
close(Vectorcardiogram_handle);

Upvotes: 4

Related Questions