I created a variable GUI function with several objects without variable names, how can I access their handles??

Here's a sample of the code. This code gets opened after pressing a button and accepting input(variable n) from a gui I created using GUIDE. Now that I have this variable gui that creates several editable text boxes, I need to access the results after they get placed inside and press another button. These objects were created without variable names, is there still a way to access them? Or did I reach a dead end and have to create something else? Thank you in advance!!
if n<10
for i=2:n
numberText=num2str(i);
uicontrol('Style','edit','Position',[nPos(1), (nPos(2)-(25*(i-1))), nPos(3), nPos(4)]);
uicontrol('Style','text','Position',[nTextPos(1), (nTextPos(2)-(25*(i-1))), nTextPos(3), nTextPos(4)],'String',("HV"+numberText),'FontSize',12,'BackgroundColor',[0.2 0.2 0.2],'ForegroundColor','w');
end
elseif n>=10
for i=2:9
numberText=num2str(i);
uicontrol('Style','edit','Position',[nPos(1), (nPos(2)-(25*(i-1))), nPos(3), nPos(4)]);
uicontrol('Style','text','Position',[nTextPos(1), (nTextPos(2)-(25*(i-1))), nTextPos(3), nTextPos(4)],'String',("HV"+numberText),'FontSize',12,'BackgroundColor',[0.2 0.2 0.2],'ForegroundColor','w');
end
for i=10:n
numberText=num2str(i);
uicontrol('Style','edit','Position',[nPos(1), (nPos(2)-(25*(i-1))), nPos(3), nPos(4)]);
uicontrol('Style','text','Position',[nTextPos(1)-8, (nPos(2)-(25*(i-1))), nTextPos(3)+8, nTextPos(4)],'String',("HV"+numberText),'FontSize',12,'BackgroundColor',[0.2 0.2 0.2],'ForegroundColor','w');
end
end

4 Comments

"These objects were created without variable names, is there still a way to access them?"
Not without writing a lot of code. The best solution by far is to always explicitly allocate all graphics handles and then use them for accessing those objects. This makes your code simpler, easier to understand, and less buggy.
I’m kind of a newbie when it comes to GUIs what did you mean by “explicitly allocate all graphics handles”? Did you mean just give it a variable name? If I were to do so, would I be able to access the handles then? Or would it be easier if i just create 12 different GUI’s?(I need between 1 and 12 text boxes depending on “n”)
I mean allocate the output of the function that creates the graphics object to a variable, e.g.:
fgh = figure(...);
or in a loop:
for k = ...
axh(k) = axes(...);
end
then you can trivially access any graphics object that you create.

Sign in to comment.

 Accepted Answer

Option 1 Assign handles to the uicontrol outputs.
h(1) = uicontrol('Style','edit', ...);
Now you can access that object via its handle h(1) if you're accessing it within the code. However, if you're trying to access a GUI object outside of the code (say, from the command line) this solution will not work unless you have access to the handles.
Option 2 Assign a 'tag' to each object. A tag is a property where you can assign a string and name the object.
h(1) = uicontrol('Style','edit', ..., 'tag', 'editBox1');
If each object has a different tag, you can search for the object using findobj(). The first input should be the figure handle that contains the object you're searching for.
findobj(gcf, 'Tag', 'editBox1')
This will return the object's handle whether you saved the handle in h(1) or not. When using this method, you should be as specific as possible when entering the search parameters in findobj() in case there happens to be multiple objects with the same tag. For example, if you know the object is an 'edit' box,
findobj(gcf, 'Style', 'edit', 'Tag', 'editBox1')

7 Comments

Since I need to access these values in the GUI1, how exactly would I access the data input into the text boxes from the current GUI2? Like this?
Inside GUI1
var=findobj(gcf,'style','edit','tag','editBox1');
get(handles.var,'Value')
Or is there any additional piece I would have to add?
On a side note, (Forgive me for the multiple questions) I have a button that will store all the info in these text boxes, but it was also created manually. How do I control what happens when it gets pushed? Would I need a callback function?
Your 'var' output is the handle. For an 'edit' object, I believe you want the text inside the box. Even if the user entered a number, the output will be a string. To get the text:
var.String
% -or -
get(var, 'String')
As for your follow-up question, yes, you'll need to assign a callback function to the button. In the callback function you'll need to get the handle of all edit boxes and extract the strings. It will be beneficial to use my option 1 instead of option 2. You could pass all of the handles to the callback function instead of searching for them.
Hi Adam, I know I already accepted the answer but I was wondering if you could answer just one more question! I have the following code for the pushbutton that needs to extract all the data from the edit boxes;
uicontrol('Style','pushbutton','String','Create Graph', ...
'Position',[550 60 80 30],'BackgroundColor','k','ForegroundColor','w', ...
'callback',{@Calculate,MO(1),MO(2),MO(3),MO(4),MO(5),MO(6),MO(7),...
MO(8),MO(9),MO(10),MO(11),MO(12),C(1),C(2),C(3),C(4),C(5),C(6),C(7),C(8),...
C(9),C(10),C(11),C(12)});
However, like you saw in the code I provided in my question, there will be times where these edit boxes don't exist depending on 'n' would this still be okay to have?, I haven't gotten any error messages on this code though. I'm only getting error messages in the callback function stating I don't have enough input arguments.
function Calculate(object_handle,event,MO1,MO2,MO3,MO4,MO5,MO6,MO7,MO8,MO9,MO10,MO11,MO12,C1,C2,C3,C4,C5,C6,C7,C8,C9,C10,C11,C12)
Am I allowed to name them anything once writing the callback function? Since I am not allowed to put parentheses MO*(1)* in the input
Hi Steve, First, I would simplify your inputs to vectors. Then I would loop through each handle in MO and C within your callback function and if the handle is empty or invalid, skip it. I'm assuming you've allocated MO and C such that they will always have a value even if the edit boxes weren't created. For example:
% Allocate
MO = zeros(1,n);
C = zeros(1,m);
% Simplify your callback inputs
uicontrol( ..., {@Calculate,MO, C});
function Calculate(src,evt, MO, C)
% Test that your handles are OK
for i = 1:length(MO)
if MO(i)~=0 && isvalid(MO(i))
content = MO(i).String;
end
end
Ah, that makes more sense to do. What exactly is 'src' and 'evt', is it necessary? Or is it essentially the same as my 'object_handle' and 'event'?
I didn't know what you named those first to inputs to the callback function. You can name them anything. If you're not using them at all you can replace either of them with tildes but they must be the first 2 inputs.
function Calculate(~,~, MO, C)

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Asked:

on 25 Jul 2018

Commented:

on 26 Jul 2018

Community Treasure Hunt

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

Start Hunting!