Formatting cell array for the 'set' function in GUI

1 次查看(过去 30 天)
This question might be a bit obvious, but I'm losing a lot of time with it...
I have a 1x2 cell like this:
strings_to_show =
'OF22' 'OF223'
I want it to convert to a variable in form of: {'OF22', 'OF223'} so that it can work in the 'set' function of the GUIs.
I would say that it is the same variable, but when I try to use the 'set' function this way:
(handleObj, 'String', {'OF22','OF223'});
it works, whereas when I try to use it like this:
(handleObj, 'String', strings_to_show );
It doesn't work.
How could I convert my 1x2 cell to the necessary format for the 'set' function??
Thank you very much for your time and effort!!!

回答(1 个)

Dimitris Iliou
Dimitris Iliou 2016-10-11
My understanding of the issue is that you want to use a cell array to set the ‘String’ argument of a graphics object. I am assuming that the graphics object will be something like a ‘popupmenu’ or a ‘listbox’.
GUIs can be created using GUIDE or the ‘uicontrol’ command.
In case you are using the ‘uicontrols’ the following example might be useful:
function myui
% Create a figure and axes
f = figure('Visible','off');
ax = axes('Units','pixels');
surf(peaks)
Colors_to_choose = {'parula','jet','hsv','hot','cool','gray'};
% Create pop-up menu
popup = uicontrol('Style', 'popup',...
'String',Colors_to_choose ,...
'Position', [20 340 100 50]);
% Make figure visble after adding all components
f.Visible = 'on';
end
If you run the code, the ‘popup’ created will contain all the values that are encapsulated in the ‘Colors_to_choose’ variables.
In case you are using GUIDE, I have attached a .FIG and a .M files that contain a very simple GUI example that has a ‘popup’ menu and a ‘listbox’. For both objects I am using the following code to set the ‘String’ value:
strings_to_show = {'OF22','OF223'};
set(hObject, 'String',strings_to_show );
So to answer your question, no special formatting is required in order to pass a cell array to a ‘set’ function.
I suggest you try running the examples and see if they work for you and what differences they have from your code when you try to use the ‘set’ command.
Finally, you can find examples of the ‘set’ command in the documentation:

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by