Opening a Figure from a Function - Not enough input arguments

Hello Reader! Basically, I am writing code that opens a figure with a button to be pressed on it, I want this button to open a figure saved in another file (same folder of course) as a function because this will keep the code tidy as a make more buttons/functions. I dont think figures works well on this online version of MATLAB.
Hello here is the function:
% Saved as flow_measurement
function flow_measurement(halfscreensizecentre,figurecolour)
figure('Name','Applied Thermofluids Calculator by Philip Schneider - Flow Measurement','Position',halfscreensizecentre,'Color',...
figurecolour,'MenuBar','none','NumberTitle','off')
end
Hello, here is part of my code:
screensize = get(groot,'ScreenSize') ;
halfscreensizecentre = [(screensize(1,[3,4])/4),(screensize(1,[3 4]))/2] ;
figurecolour = [230/255 230/255 230/255] ;
figure1 = figure('Name','Applied Thermofluids Calculator by Philip Schneider','Position',halfscreensizecentre,'Color',...
figurecolour,'MenuBar','none','NumberTitle','off')
buttoncolor = [0 64/255 115/255] ;
textcolor = [1 1 1] ;
toprowbuttonheight = 1 - 0.35 ;
toprowtextheight = toprowbuttonheight + 0.21 ;
flowmeasurementbutton = uicontrol('Style','pushbutton','Units','normalized','Position',[.125/2,toprowbuttonheight,.25,.25],'BackgroundColor',buttoncolor ...
,'Callback',@buttonfunction,'UserData','1') ; % Creates a pushbutton
% Callback assigns button press to function following @
% UserData makes an output of 1
textonflowmeasurementbutton = uicontrol('Style','text','Units','normalized','Position',[0.125/2+0.005,toprowtextheight,0.24,0.03] ...
,'String','Flow Measurement','BackgroundColor',buttoncolor,'ForegroundColor',textcolor ...
,'FontWeight','bold','FontSize',7) ;
function buttonfunction(object, ~, halfscreensizecentre,figurecolour) % function returns two outputs, object & event, event is not used so is denoted by ~
object.UserData ; % Finds out what UserData is, which is determined by which button is pressed.
if strcmpi(object.UserData,'1') % strcmp is case-insensitive string compare
disp ('Flow Measurement')
flowmeasurementfigure.Callback = {@flow_measurement,halfscreensizecentre,figurecolour} % ERROR ON THIS LINE
else
disp('How did we get here?')
end
end
I run the code press the button a recieve the following error message:
Not enough input arguments.
Error in Year_2_Applied_Thermofluids_Calculator>buttonfunction (line 48)
flowmeasurementfigure.Callback = {@flow_measurement,halfscreensizecentre,figurecolour}
Error while evaluating UIControl Callback.
Any advice on how to fix my code and achieve my desired outcome? Thank you for your time.

 Accepted Answer

If you want a callback with 4 inputs, you need to include the additional ones (beyond the first two, which are always callback source object and event structure) when you assign the callback:
flowmeasurementbutton = uicontrol('Style','pushbutton','Units','normalized','Position',[.125/2,toprowbuttonheight,.25,.25],'BackgroundColor',buttoncolor ...
,'Callback',{@buttonfunction,halfscreensizecentre,figurecolour},'UserData','1') ; % Creates a pushbutton

6 Comments

Thanks for the insight, I have changed the code and I am no longer recieving the error however, the 2nd figure is still not showing, do you know why?
When I press the button I get this output:
Flow Measurement
flowmeasurementfigure =
struct with fields:
Callback: {@flow_measurement [480 270 960 540] [0.9020 0.9020 0.9020]}
I don't see any code in buttonfunction that looks like it's intended to open a figure saved in a .fig file. To do that you would use, e.g., openfig.
I have the figure saved in a seperate file called flow_measurement (inside the file is the code in the function at the top of the question.)
I am trying to open the figure in a function written and saved in a seperate file.
Thanks
In that case, simply call flow_measurement from within buttonfunction:
function buttonfunction(object, ~, halfscreensizecentre,figurecolour) % function takes four inputs - object, event, halfscreensizecentre & figurecolour - event is not used so is denoted by ~
if strcmpi(object.UserData,'1') % strcmpi is case-insensitive string compare
disp ('Flow Measurement')
flow_measurement(halfscreensizecentre,figurecolour)
else
disp('How did we get here?')
end
end
Thank you for helping me this has worked!
I was using the code:
flowmeasurementfigure.Callback = {@flow_measurement,halfscreensizecentre,figurecolour}
your code did the job, I think I will reconsider what I was thinking, I copied the code from a youtube tutorial, there is not much resources available online so you have really helped me.
Goodbye friend...

Sign in to comment.

More Answers (1)

This line of code:
flowmeasurementfigure.Callback = {@flow_measurement,halfscreensizecentre,figurecolour} % ERROR ON THIS LINE
does not call the flow_measurement function and attempt to do something with the figure it creates. Because there is no variable named flowmeasurementfigure, that code creates a struct array named flowmeasurementfigure and sets its Callback field to a three element cell array.
If you want to create that figure, just call your flow_measurement function directly.
Also FYI, there is no figure property named Callback. There are properties that allow you to set callbacks for various operations (see the "Common Callbacks" and "Keyboard Callbacks" sections on that documentation page for some examples) but there's no Callback property like there is for uicontrol objects.

Categories

Find more on Printing and Saving in Help Center and File Exchange

Products

Release

R2023a

Asked:

on 3 Jun 2024

Commented:

on 3 Jun 2024

Community Treasure Hunt

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

Start Hunting!