필터 지우기
필터 지우기

Clear static text after press a push button

조회 수: 8 (최근 30일)
Ken
Ken 2024년 5월 28일
답변: Abhishek Kumar Singh 2024년 5월 28일
I am a newbie and I begin with creating a calculator in MATLAB GUI. I almost complete my project, just a problem I cannot solve.
I want to clear static text which displays result of a calculation after I press push button of number.
For example, I have 3 + 5, press equal button will display 8 in static text. Now I want to press number 4 and it will display 4 in static text instead of 84.
What should I do? Many thanks for your helps!

답변 (1개)

Abhishek Kumar Singh
Abhishek Kumar Singh 2024년 5월 28일
Hi @Ken
I am not familiar with the specific code you're referencing to create the calculator. Therefore, I experimented with a sample code of my own:
function calculator_gui
% Create a GUI figure
fig = figure('Name', 'Calculator', 'NumberTitle', 'off', 'Position', [200, 200, 300, 400]);
% Create a static text to display the numbers and results
result_text = uicontrol('Style', 'text', 'String', '', 'Position', [50, 320, 200, 50], 'FontSize', 20);
% Create number buttons
for i = 1:9
uicontrol('Style', 'pushbutton', 'String', num2str(i), 'Position', [50 + mod(i-1, 3)*70, 200 - floor((i-1)/3)*70, 50, 50], 'Callback', @numberButtonCallback);
end
% Create operation buttons
uicontrol('Style', 'pushbutton', 'String', '+', 'Position', [200, 200, 50, 50], 'Callback', @operationButtonCallback);
uicontrol('Style', 'pushbutton', 'String', '=', 'Position', [200, 130, 50, 50], 'Callback', @equalButtonCallback);
uicontrol('Style', 'pushbutton', 'String', 'C', 'Position', [50, 130, 50, 50], 'Callback', @clearButtonCallback);
% Initialize variables
current_expression = '';
previous_result = '';
% Callback function for number buttons
function numberButtonCallback(hObject, ~)
num = get(hObject, 'String');
if strcmp(previous_result, result_text.String)
current_expression = '';
end
current_expression = [current_expression, num];
result_text.String = current_expression;
end
% Callback function for operation buttons
function operationButtonCallback(~, ~)
current_expression = [current_expression, '+'];
result_text.String = current_expression;
end
% Callback function for equal button
function equalButtonCallback(~, ~)
result = eval(current_expression);
result_text.String = num2str(result);
previous_result = result_text.String;
current_expression = '';
end
% Callback function for clear button
function clearButtonCallback(~, ~)
current_expression = '';
result_text.String = '';
end
end
In this, the function 'numberButtonCallback'checks whether the previous results is displayed in the static text ('result_text.String'). If the pervious result is displayed, it means the user just completed an operation and wants to start a new expression. In this case, it clears the 'current_expression' variable to start fresh.
Then it appends the pressed number to the 'current_expression' variable and finally it updates the static text ('result_text.String') to display the updated 'current_expression'.
I hope it helps!

태그

제품


릴리스

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by