Problem getting handles structure to update in GUI

조회 수: 1 (최근 30일)
Michael
Michael 2011년 6월 7일
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