Azzera filtri
Azzera filtri

Opening a Figure from a Function - Not enough input arguments

2 visualizzazioni (ultimi 30 giorni)
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',...
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',...
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
disp('How did we get here?')
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.

Risposta accettata

Voss il 3 Giu 2024
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 Commenti
Philip il 3 Giu 2024
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...

Accedi per commentare.

Più risposte (1)

Steven Lord
Steven Lord il 3 Giu 2024
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.


Scopri di più su Interactive Control and Callbacks in Help Center e File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by