Not enough input arguments when using setappdata

조회 수: 2 (최근 30일)
Jordan Robinson
Jordan Robinson 2018년 5월 4일
답변: lokender Rawat 2018년 5월 8일
I am using a uicontrol pushbutton to start an animation of a wheel spinning when the button is pressed by the player. the callback function called wheelspin handles the animation, and it keeps track of how much the wheel has rotated in the variable rot. I need this variable as an output so that I can work with it in my main code. my understanding is that I can use getappdata and setappdata to achieve this. However, I am getting an error: "Not enough input arguments", citing the line with setappdata. Below is a simplified version of my code
from my main code:
fig = figure;
spinit = uicontrol('style', 'pushbutton', 'String', 'Spin the Wheel', 'position', [20 0 50 20], 'Callback',
@wheelspin);
getappdata(fig,'rot');
from the callback function
function wheelspin(wheel, source,event,fig)
rot = 0
spin = round(randn*100);
for k = 1:spin
rot = rot+15
end
setappdata(fig,'rot',rot)
Thank you for your help.

답변 (1개)

lokender Rawat
lokender Rawat 2018년 5월 8일
The error you are getting is because of the incorrect way of passing parameters to the function 'wheelspin' from the 'main' function. The general syntax of passing arguments to the callback function is using list of comma separated parameters after the function name prefixed by '@' operator: Example,
uicontrol('Style', 'pushbutton', 'String', 'Spin the Wheel', 'position', [20 340 100 50], 'Callback',{@wheelspin,fig});
Here, we have passed just one parameter i.e. 'fig' to the function 'wheelspin'. You can then use the 'setappdata' in the callback function to set any variable with value. You can modify your code as follows :
main.m
function main()
fig = figure;
spinit = uicontrol('Style', 'pushbutton', 'String', 'Spin the Wheel', 'position', [20 340 100 50],'Callback',{@wheelspin,fig});
getappdata(fig,'rot');
end
wheelspin.m
function wheelspin(source,event,fig)
rot = 0;
spin = round(randn*100);
for k = 1:spin
rot = rot+15;
end
setappdata(fig,'rot',rot);
end
Note: There is no sense in passing the variable 'wheel'. It does not store anything in your main code. Even if you are using that in your main code, but that need not be passed to the 'wheelspin' function as there is no computation involved using that variable in the 'wheelspin' function.
However, if you wish to use the 'wheel' variable later in your 'wheelspin' function, you can pass it the same way 'fig' has been passed using the below command:
uicontrol('Style', 'pushbutton', 'String', 'Spin the Wheel', 'position', [20 340 100 50], 'Callback',{@wheelspin,fig,wheel});

카테고리

Help CenterFile Exchange에서 App Building에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by