Code still functioning according to lines of code I have deleted

5 vues (au cours des 30 derniers jours)
Ruben
Ruben le 30 Juil 2024
Réponse apportée : Aditya le 31 Juil 2024
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 commentaire
VBBV
VBBV le 30 Juil 2024
@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.

Connectez-vous pour commenter.

Réponse acceptée

Aditya
Aditya le 31 Juil 2024
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.

Plus de réponses (0)

Catégories

En savoir plus sur Develop uifigure-Based Apps dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by