function within a gui set function

2 views (last 30 days)
Ian
Ian on 11 Dec 2014
Commented: Ian on 11 Dec 2014
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
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.
Ian
Ian on 11 Dec 2014
Gotcha, thank you for your help!

Sign in to comment.

More Answers (0)

Categories

Find more on Entering Commands in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!