필터 지우기
필터 지우기

Opening a Figure from a Function - Not enough input arguments

조회 수: 2 (최근 30일)
Philip
Philip 2024년 6월 3일
댓글: Philip 2024년 6월 3일
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.

채택된 답변

Voss
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
Philip
Philip 2024년 6월 3일
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...
Voss
Voss 2024년 6월 3일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
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 CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by