Code still functioning according to lines of code I have deleted

조회 수: 5 (최근 30일)
Ruben
Ruben 2024년 7월 30일
답변: Aditya 2024년 7월 31일
I'm trying to make a text box that the user can type into, which then retreives and prints that text when the user clicks enter. Two days ago it was working perfectly. Yesterday it stopped working properly. It prints the currently held text AND THEN updates with the what the user has input. So if I type in "bob" and click enter, nothing displays. If I delete "bob" and type in "peter", and click enter, it displays bob. Click enter again, and it displays "peter".
It got to the point that I just deleted the code and tried to start over. FOR SOME REASON, it still acts exactly the same, updating the text only when I click enter, despite the fact that I have no lines of code that refer to the enter key.
This is what I have right now:
function codeComponentResponse
fig = uifigure('position',[2 50 637 641]);
TextArea = uieditfield(fig, 'Position',[100 100 500 30]);
fig.WindowKeyPressFcn = {@CoolGuy, fig, TextArea};
function CoolGuy(src, event, figure, field)
text = field.Value;
disp(text)
end
end
  댓글 수: 1
VBBV
VBBV 2024년 7월 30일
@Ruben If I delete "bob" and type in "peter", How did you delete "bob" in the text field ? Using keys or mouse ?
Try to enter twice after typing the text "bob" in edit field , it works fine.

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

채택된 답변

Aditya
Aditya 2024년 7월 31일
Hi Ruben,
The issue you're experiencing may be due to the timing of event handling in MATLAB. Specifically, the "WindowKeyPressFcn" might be capturing the key press event before the "uieditfield" has updated its value. To ensure the text is updated correctly, you can use a different callback function that triggers after the text is modified.
Here’s an improved version of your code using the "ValueChangedFcn" of the "uieditfield" to ensure the text is updated correctly when the enter key is pressed:
function codeComponentResponse
fig = uifigure('Position', [2 50 637 641]);
TextArea = uieditfield(fig, 'text', 'Position', [100 100 500 30]);
% Set the callback for when the text value changes
TextArea.ValueChangedFcn = @(src, event) CoolGuy(src, event, TextArea);
function CoolGuy(src, event, field)
text = field.Value;
disp(text)
end
end
This approach ensures that the displayed text is updated correctly and immediately after the user presses enter.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Develop uifigure-Based Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by