I trying to get a check box to work on a gui

1 visualizzazione (ultimi 30 giorni)
Amanda
Amanda il 1 Dic 2022
Commentato: Image Analyst il 2 Dic 2022
I have created a gui for a robot maze. I have a check box, when its checked i want it to show the bath of the robot when its not I dont want it to show the path. This is the function I a have written for it but it doesn't do what I need it to do.
createRandomMaze
figure;
first_button = uicontrol('style', 'pushbutton');
set(first_button, 'units', 'normalized', 'position', [0.2,0.7, 0.2, 0.2])
set(first_button, 'string', 'turn left')
set(first_button, 'callback', @turnLeft)
second_button = uicontrol('style', 'pushbutton');
set(second_button, 'units', 'normalized', 'position', [0.4, 0.7, 0.2, 0.2])
set(second_button, 'string', 'step')
set(second_button, 'callback', @step)
third_button = uicontrol('style', 'pushbutton');
set(third_button, 'units', 'normalized', 'position', [0.6, 0.7, 0.2, 0.2])
set(third_button, 'string', 'turn right')
set(third_button, 'callback', @turnRight)
fourth_button = uicontrol('style', 'pushbutton');
set(fourth_button, 'units', 'normalized', 'position', [0.2, 0.5, 0.2, 0.2])
set(fourth_button, 'string', 'left')
set(fourth_button, 'callback', @stepLeft)
fifth_button = uicontrol('style', 'pushbutton');
set(fifth_button, 'units', 'normalized', 'position', [0.4, 0.5, 0.2, 0.2])
set(fifth_button, 'string', 'back')
set(fifth_button, 'callback', @stepBack)
sixth_button = uicontrol('style', 'pushbutton');
set(sixth_button, 'units', 'normalized', 'position', [0.6, 0.5, 0.2, 0.2])
set(sixth_button, 'string', 'right')
set(sixth_button, 'callback',@stepRight)
seventh_button = uicontrol ('style','checkbox');
set(seventh_button, 'units', 'normalized', 'position', [0.2, 0.3, 0.2, 0.2])
set(seventh_button, 'string', 'History')
set(seventh_button, 'callback', @toggleHistory)
eighth_button = uicontrol ('style', 'edit');
set(eighth_button, 'units', 'normalized', 'position', [0.4, 0.37, 0.2, 0.05])
set(eighth_button, 'string', '2')
ninth_button = uicontrol('style','pushbutton');
set(ninth_button, 'units', 'normalized', 'position', [0.6, 0.3, 0.2, 0.2])
set(ninth_button, 'string', 'Run!')
set(ninth_button, 'callback', @Run)
ninth_button.Callback = 'number_of_steps = str2num(eighth_button.String); walk(number_of_steps)';
function toggleHistory(sender, eventdata)
if sender.Value == 1
disp showHistory
else
disp hideHistory
end
end
function Run(sender, eventdata)
step_input = (['eighth_button', 'string']);
if ~strcmp(step_input, ' ');
number_of_steps = str2num(step_input);
walk(number_of_steps);
end
end
function turnRight
turnLeft();
turnLeft();
turnLeft();
end
function stepLeft
turnLeft();
step();
turnRight();
end
function stepRight
turnRight();
step();
turnRight();
end
function stepBack
turnLeft();
turnLeft();
step();
turnRight();
end
function turnLeft
turnRight();
turnRight();
turnRight();
step
end
function step
step();
end
function steps_made_successfully = walk(number_of_steps)
steps_made_successfully = 0;
for i=1:number_of_steps
if is_clear()steps_made_successfully = steps_made_successfully + 1;
move_forward();
else
break;
end
end
end

Risposte (1)

Image Analyst
Image Analyst il 2 Dic 2022
As I check or uncheck the box (badly called "seventh_button") it does display the words according to the value of the checkbox:
showHistory
hideHistory
showHistory
hideHistory
showHistory
hideHistory
so I'm not sure what you're asking. It seems to do what you told it to do.
  2 Commenti
Amanda
Amanda il 2 Dic 2022
In the robot maze it shows a line following the robot I need it to be there when you check it and disappear when its not checked
Image Analyst
Image Analyst il 2 Dic 2022
You need to get the handle to the line when you plot it.
plotHandle = plot(x, y);
Then call
delete(plotHandle)
when you want to erase the line. Then when the box is checked call plot again.

Accedi per commentare.

Categorie

Scopri di più su Migrate GUIDE Apps in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by