How to use a edit box on a GUI

조회 수: 14 (최근 30일)
Amanda
Amanda 2022년 12월 1일
댓글: Amanda 2022년 12월 1일
I am trying to use a edit box to be able to put a number in the box and then use a push button and it will take that number and use it. I am doing this for a robot maze and im trying to write a number of steps in the edit box and the when i push the button it takes that many steps in the maze. I'm suppose to use the a function that has already been created 'walk' to do this but i have no idea how to.
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
  댓글 수: 2
Benjamin Thompson
Benjamin Thompson 2022년 12월 1일
Is this a GUIDE GUI or AppDesigner GUI? You may want to attach your current work for the Community to reference.
Amanda
Amanda 2022년 12월 1일
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)
function toggleHistory(sender, eventdata)
if sender.Value == 1
disp showHistory
else
disp hideHistory
end
end
function robotRun(sender, eventdata)
step_input = get('edit', '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

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

채택된 답변

Kevin Holly
Kevin Holly 2022년 12월 1일
Add a callback to the ninth_button that reads the value in the 'eighth_button' edit field and places it as an input to the walk function.
ninth_button.Callback = 'number_of_steps = str2num(eighth_button.String); walk(number_of_steps)';
  댓글 수: 1
Amanda
Amanda 2022년 12월 1일
would you also know how to fix my toggle history so when its clicked it shows when the robot moves and when its unclicked it doesnt

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by