Code still functioning according to lines of code I have deleted
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commento
VBBV
il 30 Lug 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.
Risposta accettata
Aditya
il 31 Lug 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.
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Develop uifigure-Based Apps in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!