I just made a file for the code because of the inconvenience reading the code in website.
[weird issue] Matlab behaves different with/without breakpoints
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
In my code there is a GUI with an edit box, whose KeyPressFcn is redirected into the function I'm presenting.
What I am trying to do is to judge the key-press into the edit box, and if you press a number key, you replace the string in the edit box into the number; otherwise you discard this edit (and if it is a functional key you don't do anything).
I already have the PrevString defined in the CreateFcn as '0'.
And here is the function:
function AllEditBoxKeyPressFcn(hObject, eventdata, handles)
if isempty(eventdata.Character);
elseif eventdata.Character <= '9' && eventdata.Character >= '0';
uiwait(hObject.Parent, 1);
hObject.String = eventdata.Character;
else
A = getappdata(hObject, 'PrevString');
uiwait(hObject.Parent, 1);
hObject.String = A;
end
setappdata(hObject, 'PrevString', hObject.String);
end
When I am debugging, I try to type numbers first. It works just fine. But when I type something like 'a' or 'd', it just takes it, and what remains in the edit box is something like 'd3' or '8a'. However, when I try to make breakpoints in the code, it works perfectly.
So why does it skip statements? I don't think it is because it responses to slowly, instead I believe something really goes wrong.
Please help me out. Any help or suggestion will be appreciated.
Antworten (1)
Walter Roberson
am 16 Nov. 2015
The visible String property of a uicontrol Edit does not change if you manipulate the property in a KeyPressFcn callback, not until you press Return or you move the focus to something else. When you take a breakpoint, you sort of move the focus to something else. But more specifically, when you take a breakpoint, before the keyboard is given control, drawnow() is called.
Unless they changed the behaviour in R2014b or later (and evidence I have seen from postings is that they more or less left it alone), changing the String property of the Edit control is not going to work the way you would like. I do not know why that design decision was made. If you need to do character-by-character processing of an Edit uicontrol then you are going to need to go in at the Java level.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Entering Commands finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!