Problem getting handles structure to update in GUI

1 次查看(过去 30 天)
I am having a problem with the handles structure in a GUI. What I am doing is keeping a log of the current text entered by the user. Then, when an event is triggered, I plot this text on an axes along with a couple of line objects. Here is the code I am using to do this:
function addPhone(hObject, x)
handles = guidata(hObject);
set(handles.phoneInput_edittext,'String',[]);
m = get(handles.axes2,'YLim');
l1 = line([x(1) x(1)], m, 'Color', 'b', 'Parent', handles.axes2);
l2 = line([x(2) x(2)], m, 'Color', 'b', 'Parent', handles.axes2);
t = text(mean(x), mean([m(1) mean(m)]), 'String', handles.updStr, 'Color', 'b', ...
'HorizontalAlignment', 'center', 'Parent', handles.axes2);
handles.updStr = [];
guidata(hObject, handles);
I am also setting the String of the EditText back to empty, and also the variable handles.updStr, which contains my user string.
There are two problems. The first is that the handles structure for some reason does not update from within this function, so when the function ends, handles.updStr is not empty - it still contains the user input string.
The second problem is that there are intermittent problems accessing the handles class from within the function because text() sometimes behaves as expected, printing the text on the screen, and sometimes returns an error saying "Invalid parameter/value pair arguments", which I believe refers to a problem with the data I'm trying to use in the handles structure.
Does anyone know the problem here?

采纳的回答

Gerd
Gerd 2011-6-7
Hi Michael,
I also have sometimes a strange behaviour using the guidata(hObject) terminology. I started using for my own data
mydata = getappdata(gcf, 'mydata'); and
setappdata(gcf,'mydata',mydata);
You can save all your variables in the mydata structure. That helped a lot.
But be carefull always using the right figure(gcf).
Gerd

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by