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')

 채택된 답변

Adam
Adam 2014년 12월 11일
편집: Adam 2014년 12월 11일

0 개 추천

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

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.
Ian
Ian 2014년 12월 11일
Ok interesting. So it converted my tag string names into a numerical tag?
Adam
Adam 2014년 12월 11일
편집: Adam 2014년 12월 11일
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 2014년 12월 11일
Gotcha, thank you for your help!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Entering Commands에 대해 자세히 알아보기

태그

질문:

Ian
2014년 12월 11일

댓글:

Ian
2014년 12월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by