UserK
UserK

Reputation: 908

MATLAB GUI- Setting string property returns 'Invalid or deleted object.'

I am having trouble setting a specific property of an object in a MATLAB gui. Sometimes the script returns the error

Invalid or deleted object.

Here is text field I'd like to change.

conTxt = uicontrol('Style','text', 'String','Offline','ForegroundColor',[.99 .183 0.09], ...
        'Position', [70 20 100 30],...
        'Parent',hTabs(1), 'FontSize',13,'FontWeight','bold');

I've set up a serial communication in which the text field is used as feedback for the user. When a response arrives from the serial this line is executed:

set(conTxt,'ForegroundColor', [.21 .96 .07],'String','Online');

Do you know how to fix it?

Upvotes: 0

Views: 1105

Answers (1)

Benoit_11
Benoit_11

Reputation: 13945

I'm posting this as an answer because a comment would be too long and ugly.

From the comments it looks like the GUI does not recognize the text box since it is not in its handles structure, therefore when a callback is executed the GUI does not know where to look for the element. What if you try the following:

1) Store the components in the handles structure, something like:

handles.conTxt = uicontrol('Style','text', 'String','Offline','ForegroundColor',[.99 .183 0.09], ...
        'Position', [70 20 100 30],...
        'Parent',hTabs(1), 'FontSize',13,'FontWeight','bold');

and so on for other components.

2)At the end of the setup of your programmatic GUI, update the handles structure with the guidata property of the GUI:

guidata(handles.figure,handles);

3) Then at the beginning of each callback, use something like this:

handles = guidata(gcf);

to fetch the handles structure and access its components.

Hope that helps!

Upvotes: 1

Related Questions