MATLAB Answers

control of switch button with another switch button

5 views (last 30 days)
belhassan haj mansour
belhassan haj mansour on 9 Jan 2019
Commented: OCDER on 10 Jan 2019
HELLO
im trying to control a rockerswitch button with another one in app designer
details: i have rockerswitch1 in the position 'off' and rockerswitch2 also in position 'off' . what i need is when i put rockerswitch1 in position 'on', the rockerswitch 2 become in the position 'on' too but i can't find the right function to do so. Also i tried to properly call the handle and use it within the if statement but i couldn't do it the right way
CAN ANYONE HELP ME WITH THIS PLEASE ?

  0 Comments

Sign in to comment.

Accepted Answer

OCDER
OCDER on 9 Jan 2019
function test
Gx = uifigure('Position',[100 100 350 275]);
% Create switch1
UI1 = uiswitch(Gx, 'rocker',...
'Tag', 'switch1', ...
'Items', {'On', 'Off'},...
'Position', [140 160 20 45],...
'ValueChangedFcn', @switchMoved);
% Create switch2
UI2 = uiswitch(Gx, 'rocker',...
'Tag', 'switch2', ...
'Items', {'On', 'Off'},...
'Position', [180 160 20 45],...
'ValueChangedFcn', @switchMoved);
%Use this callback function to run when there is a value changed in switch1 or 2
function switchMoved(src, event)
switch src.Tag
case 'switch1' %change switch2 too
UI2.Value = UI1.Value;
case 'switch2' %change switch1 too
UI1.Value = UI2.Value;
end
end
end

  3 Comments

belhassan haj mansour
belhassan haj mansour on 9 Jan 2019
it still dosen't work
maybe because i want to use this callback inside of a list box
PS i'm using matalb 2018a
OCDER
OCDER on 10 Jan 2019
You can't put a function within a switch statement. Also, consider simplifying your cases for your switch. They're rather long... Lastly, upload the code, not an image of your code. It's easier if we can copy paste code, and it'll be easier for you too to Ctrl+C and Ctrl+V instead of PrintScreening.
A callback function is summoned when you do an event (like clicking the rocker switch). Try the test function I gave you first before building app. Does it work? Your version 2018a supports it.
switch Option
case 'A'
case 'CHECK THIS CASE STATEMENT AS IT IS VERY LONG & WILL MAKE LIFE DIFFICULT'
function switchedove(varargin) %NOPE. This can't be done.
end
end
end

Sign in to comment.

More Answers (0)