Opening a Figure from a Function - Not enough input arguments
2 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Voss
2024-6-3
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 个评论
更多回答(1 个)
Steven Lord
2024-6-3
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!