lag with "figure" function

3 views (last 30 days)
j dr
j dr on 6 Apr 2011
I have devised this function in order to make new figures appear where I want them to.
function figuree(varargin)
tic
if ~isempty(varargin)
if varargin{:}>0 && length(varargin{:})<2
figure(varargin{1})
else
error('illegal figuree arguments \n Arguments must be the same as figure')
end
else
toc
figure()
toc
end
set(gcf,'OuterPosition',[1,470,500,558])
This goes perfectly well.
But when I make for example this call:
close all
clear all
figure
figure
figure
figuree(2)
figuree
figuree(2)
figuree
this is my output:
Elapsed time is 0.000186 seconds.
Elapsed time is 0.260609 seconds.
Elapsed time is 0.000017 seconds.
Elapsed time is 3.168427 seconds.
My question is why do I have a sudden 3 second delay ??! (I will also accept advice about how to make new figure appear where I want them to)

Accepted Answer

Matt Fig
Matt Fig on 19 Apr 2011
I have no idea why you are experiencing this huge delay. I put your exact code into an M-File and saved it. Then, pasting your code to the command line:
>> close all
clear all
figure
figure
figure
figuree(2)
figuree
figuree(2)
figuree
Elapsed time is 0.000473 seconds.
Elapsed time is 0.133531 seconds.
Elapsed time is 0.000035 seconds.
Elapsed time is 0.130823 seconds.
And these results are consistent. I can paste as many times as I like and see nearly the same numbers. I seriously doubt the event queue takes that long to flush because you start out with no graphics objects (the close all makes sure of that), and all figures are blank! Something else is going on....
As an alternative to using VARAGIN within the function the way you are, you could use NARGIN.
function figuree(NUM)
% Custom figure creation.
N = nargin;
if N==1
figure(NUM)
elseif N==0
figure()
end
set(gcf,'OuterPosition',[1,470,500,558])
Also, if you want your figures to always be there, put the default value assignment in your startup file. If you don't already have a startup file, just create one and make sure it is on your path. Every time you run MATLAB after that, startup.m will execute and set your figure default position.
  1 Comment
Walter Roberson
Walter Roberson on 19 Apr 2011
Or
if exist('NUM','var')

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 6 Apr 2011
Have you considered
set(0,'DefaultFigureOuterPosition', [1,470,500,558])
figure() can take a variable length of time because MATLAB processes the event queue each time figure() is called.
  3 Comments
j dr
j dr on 19 Apr 2011
indeed, any figure called after calling my script:
close all
clear all
figure
figure
figure
figuree(2)
figuree
figuree(2)
%%%%
tic
figure
toc
will take 3 seconds... Is it that I filled the "queue" with my previous callings ? Is this due to my supposed misuse of varargin?
Also the varargin works for me. The figure function is not supposed to be called with multiple arguments, only with either 0 or 1 arguments and I couldn't find any other way to make it work for both these cases.
Please keep me posted !

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!