MATLAB: Does it not work to close the figure programmatically in MATLAB 7.11 (R2010b)

close figurehandle graphicsinfoboxMATLAB

I am using this code in MATLAB 7.11 (R2010b) to create a infobox:
function g = infobox(text, header, notneeded)
if nargin < 2
header = 'Info box';
end
relWidth = 0.5;
relHeight = 0.1;
screenSize = get(0, 'ScreenSize');
height = min(1.1*1024,
screenSize(4));
width = min(1.1*1280, screenSize(3));
% Position of is relative to screen resolution
pos = [width*(1-relWidth)/2, height*(1- relHeight)/2, ...
width*relWidth, height*relHeight];
gColor = [0.92941 0.94118 0.9451];
g = figure('NumberTitle', 'off', ...
'MenuBar', 'none', ...
'Resize', 'off', ...
'Name', header, ...
'Color', gColor, ...
'Position', pos, ...
'WindowStyle', 'modal');
set(g,'HandleVisibility','off')
pp = getpixelposition(g);
uicontrol(g, 'Style', 'text', ...
'String', text,...
'HorizontalAlignment', 'left', ...
'BackgroundColor', get(g,'Color'),...
'FontName', 'Calibri', ...
'FontSize', 14,...
'Position', [0.1*pp(3) 0.05*pp(4) 0.8*pp(3) 0.9*pi(4)]);
pause(1)
end
This infobox should close after a while but this does not work:
infobox('test');
close(gcf)

Best Answer

You set the 'HandleVisibility' property of the figure to 'off'. When you do this, gcf does not see the figure. Here is your code (formatted for others to be able to read it!) and how to call it
function g = infobox(text, header, notneeded)
if nargin < 2
header = 'Info box';
end
relWidth = 0.5;
relHeight = 0.1;
screenSize = get(0, 'ScreenSize');
height = min(1.1*1024, screenSize(4));
width = min(1.1*1280, screenSize(3));
% Position of is relative to screen resolution
pos = [width*(1-relWidth)/2, height*(1- relHeight)/2,width*relWidth, height*relHeight];
gColor = [0.92941 0.94118 0.9451];
g = figure('numbertitle', 'off', 'MenuBar', 'none', ...
'resize', 'off', 'name', header, ...
'Color',gColor, 'position',pos,...
'windowstyle', 'modal');
set(g,'HandleVisibility','off');
pp = getpixelposition(g);
R = uicontrol(g, 'Style', 'text', ...
'String',text,...
'HorizontalAlignment', 'left', ...
'BackgroundColor',get(g,'Color'),...
'FontName', 'Calibri', 'FontSize', 14,...
'Position', [0.1*pp(3) 0.05*pp(4) 0.8*pp(3) 0.9*pp(4)]) ;
pause(1)
end %%%END CODE%%%
Now when you call it from the command line and you want to close it, use:
g = infobox('test');close(g)
Also, there is an error in the 'position' property of your textbox. You should have written pp(4) as I show above, not pi(4).