Code still functioning according to lines of code I have deleted

2 次查看(过去 30 天)
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