function within a gui set function

Is it possible call upon a tag name in a gui which is dependent on a changing number? Here's my code and what I'm trying to do:
Upon opening, I set i=[0 0 0 0]. As the zeros turn to 1s in the vector, I want to change the color of different text boxes. The tags on my text boxes are named 'attempt1_1' 'attempt1_2' 'attempt1_3' and so on. In the set function, is there a way to call upon a variable to change the "number" in handles.attempt1_number?
function Red_p_Callback(hObject, eventdata, handles)
% hObject handle to Red_p (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
nextempty=find(i==0);
set(handles.attempt1_nextempty(1),'BackgroundColor','r')

 Accepted Answer

Adam
Adam on 11 Dec 2014
Edited: Adam on 11 Dec 2014
You can manipulate the string and apply that, but I would favour bundling your text box handles together into an array of handles as e.g.
handles.editHandles = [ handles.attempt1_1, handles,attempt1_2, handles.attempt1_3,...];
Then just index into that array to changed the one you want e.g.
i = [0 0 0 0];
for n = 1:4
i(n) = 1;
set( handles.editHandles(n), 'BackgroundColor', 'r' )
end
You should be able to manipulate tags as follows if you prefer:
tag = sprintf( 'attempt1_%d', n )

5 Comments

So I tried that first line you gave me: handles.editHandles=[handles.attempt1_1, handles.attempt1_2, handles.attempt1_3, handles.attempt1_4]
I didn't suppress it to make sure it works, but this is what I got in the command window handles =
figure1: 182.0078
attempt1_4: 13.0087
attempt1_3: 12.0087
attempt1_2: 11.0087
attempt1_1: 10.0087
Yellow_p: 9.0087
Green_p: 8.0087
Blue_p: 7.0094
Red_p: 183.0078
output: 182.0078
editHandles: [10.0087 11.0087 12.0087 13.0087]
Where did I go wrong?
That looks fine. As you can see
handles.editHandles
now contains the 4 edit box handles given higher up.
Ok interesting. So it converted my tag string names into a numerical tag?
Adam
Adam on 11 Dec 2014
Edited: Adam on 11 Dec 2014
No, your tags are still there and can be used if you want, but it simply puts all the handles into an array so you can numerically index into that array just like any other array rather than having to manipulate numbers to strings and fish out ui components by the tag that you then create.
So I guess in essence it does convert your tag string names to numerical tags from a usage perspective, just not from an engineering perspective!
Having ui handles in an array like that allows you to set a property of all of them at once too.
set( handles.editHandles, 'BackgroundColor', 'r' )
would set all of them to red background in a single statement.
Or
set( handles.editHandles([1,3]), 'BackgroundColor', 'r' );
would set numbers 1 and 3 to red in a single statement.
Gotcha, thank you for your help!

Sign in to comment.

More Answers (0)

Categories

Tags

Asked:

Ian
on 11 Dec 2014

Commented:

Ian
on 11 Dec 2014

Community Treasure Hunt

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

Start Hunting!