How can i change a GUI toggle state using values from a separate function?

2 次查看(过去 30 天)
I am creating a flight simulator GUI and I would like to change the state of a toggle switch based off of the status of a joystick button. If the joystick button is depressed (returns a value of '1'), I would like the toggle switch to also represent that state ('Value', 'Max'). How would I go about retrieving the joystick value within the toggle button call back function(tb1_Callback)? Any help would be greatly appreciated. Thanks!!
joy = vrjoystick(1);
while true
[axes, buttons, povs] = read(joy);
roll = (180+(axes(1)*180))/2;
pitch = (180-(axes(2)*180))/2;
yaw = (180+(axes(3)*180))/2;
throttle = (180-(axes(4)*180))/2;
trigger = buttons(1);
HUD = povs;
bar(handles.axes1, (axes(2)*100), 1, 7,'y');
set(handles.axes1, 'XLim', [-103.5 103.5]);
set(handles.axes1, 'YLim', [0 1]);
set(handles.axes1, 'YTick', [0:5:5]);
set(handles.axes1, 'XTick', [-100:25:100]);
set(handles.axes1, 'Color', [.75 .75 .75]);
title(handles.axes1, 'ELEVATOR', 'FontSize', 10, 'FontWeight', 'bold');
bar(handles.axes2, (axes(1)*100), 1, 7,'y');
set(handles.axes2, 'XLim', [-103.5 103.5]);
set(handles.axes2, 'YLim', [0 1]);
set(handles.axes2, 'YTick', [0:1:1]);
set(handles.axes2, 'XTick', [-100:25:100]);
set(handles.axes2, 'Color', [.75 .75 .75]);
title(handles.axes2, 'ALERION', 'FontSize', 10, 'FontWeight', 'bold');
bar(handles.axes3, (axes(3)*100), 1, 7, 'y');
set(handles.axes3, 'XLim', [-103.5 103.5]);
set(handles.axes3, 'YLim', [0 1]);
set(handles.axes3, 'YTick', [0:1:1]);
set(handles.axes3, 'XTick', [-100:25:100]);
set(handles.axes3, 'Color', [.75 .75 .75]);
title(handles.axes3, 'RUDDER', 'FontSize', 10, 'FontWeight', 'bold');
barh(handles.axes4,((100-(axes(4)*100))/2),1, 7, 'g');
set(handles.axes4, 'XLim', [0 1]);
set(handles.axes4, 'YLim', [-3.5 103.5]);
set(handles.axes4, 'XTick', [0:5:5]);
set(handles.axes4, 'YTick', [0:25:100]);
set(handles.axes4, 'Color', [.75 .75 .75]);
title(handles.axes4, 'THROTTLE', 'FontSize', 8, 'FontWeight', 'bold');
barh(handles.axes5,((100-(axes(4)*100))/2),1, 7, 'g');
set(handles.axes5, 'XLim', [0 1]);
set(handles.axes5, 'YLim', [-3.5 103.5]);
set(handles.axes5, 'XTick', [0:5:5]);
set(handles.axes5, 'YTick', [0:25:100]);
set(handles.axes5, 'Color', [.75 .75 .75]);
title(handles.axes5, 'THROTTLE', 'FontSize', 8, 'FontWeight', 'bold');
barh(handles.axes6,((100-(axes(4)*100))/2),1, 7, 'g');
set(handles.axes6, 'XLim', [0 1]);
set(handles.axes6, 'YLim', [-3.5 103.5]);
set(handles.axes6, 'XTick', [0:5:5]);
set(handles.axes6, 'YTick', [0:25:100]);
set(handles.axes6, 'Color', [.75 .75 .75]);
title(handles.axes6, 'FLAPS', 'FontSize', 8, 'FontWeight', 'bold');
barh(handles.axes7,((100-(axes(4)*100))/2),1, 7, 'g');
set(handles.axes7, 'XLim', [0 1]);
set(handles.axes7, 'YLim', [-3.5 103.5]);
set(handles.axes7, 'XTick', [0:5:5]);
set(handles.axes7, 'YTick', [0:25:100]);
set(handles.axes7, 'Color', [.75 .75 .75]);
title(handles.axes7, 'FLAPS', 'FontSize', 8, 'FontWeight', 'bold');
a.servoWrite(3, round(roll));
a.servoWrite(5, round(pitch));
a.servoWrite(6, round(yaw));
a.servoWrite(9, round(throttle));
if(HUD == 90 || HUD == 45 || HUD == 135)
a.servoWrite(10, 0);
else a.servoWrite(10, 90);
end
if(HUD == 270 || HUD == 315 || HUD == 225)
a.servoWrite(10, 180);
end
if(HUD == 0 || HUD == 45 || HUD == 315)
a.servoWrite(11, 0);
else a.servoWrite(11, 90);
end
if(HUD == 180 ||HUD == 135 || HUD == 225)
a.servoWrite(11, 180);
end
pause(.001);
a.flush;
if(buttons(12) == 1 && buttons(11) ==1 && buttons(2) == 1)
delete(instrfind({'Port'},{'COM4'}));
delete(instrfind('Type', 'serial'));
close all
clc
break
end
end
% --- Executes on button press in tb1. function tb1_Callback(hObject, eventdata, handles)
% Hint: get(hObject,'Value') returns toggle state of tb1

采纳的回答

alex korzaniewski
In hind sight this was a bit of a naive question and a little bit of playing around got me the desired result. I had to place the code within the 'while' loop which read in joystick data. In my case b7 corresponds with button 7 on the joystick.
if (b7 == 1);
set(handles.tb7, 'Value', 1);
set(handles.tb7, 'BackgroundColor', 'y');
else (b7 == 0);
set(handles.tb7, 'Value', 0);
set(handles.tb7, 'BackgroundColor', [.94 .94 .94]);
end
Hope this helps anyone facing a similar situation.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Axis Labels 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by