MATLAB: GUI created by GUIDE returns different data types for uicontrol String property.

While building a GUI created by GUIDE, if I add a user interface control (uicontrol) such as an edit text component to the GUI using GUIDE, and later use the get command to retrieve the value of its String property in the GUI MATLAB file, Why does it return a cell array sometimes and a character array other times?

Best Answer

The return type depends on the way the 'String' property was set on the uicontrol created by GUIDE. 
If you create a uicontrol in GUIDE and have not changed its String property, the 'get' command returns the 'String' value as a cell array.
If you change the 'String' property using the Property Inspector, the type of the returned data could be either cell array or character array depending on whether you edited the displayed string directly or you clicked the button to use the standalone string editor dialog box.
In order to enforce a certain type, you can add a statement similar to one of the following to the 'uicontrol' CreateFcn callback:
% Enforce character array type
>> set(hObject,'String','desired text');
% Enforce cell array type
>> set(hObject,'String',{'desired text'});
If later you change the value of the String property using the "set" command in your GUI M-file, 'get' returns the value as the type given by the 'set' command. If you change it interactively, e.g., by editing the contents of an edit text component, the data type is unchanged and 'get' returns the value as the type it had before the change.