필터 지우기
필터 지우기

how to use string within a line of code

조회 수: 4 (최근 30일)
Peng Zhao
Peng Zhao 2017년 9월 1일
댓글: Adam 2017년 9월 1일
I was trying to use a string as a object handle within a for loop to save space as following:
function pushbutton1_Callback(hObject, eventdata, handles)
for Button = 1
Button = num2str(Button);
Obj = strcat('handles.Num',Button);
set(str2func(Obj), 'ForegroundColor','red')
set(handles.Num1, 'ForegroundColor','red')
end
I would like to use Obj to replace handles.Num1. is there any way to do this?

채택된 답변

Adam
Adam 2017년 9월 1일
handles.( [ 'Num', num2str( button ) ] )
should work, although quite what you are doing with Button I can't work out
for Button = 1
is pointless and
Button = num2str(Button);
overwriting the loop variable is also not good code.
  댓글 수: 2
Peng Zhao
Peng Zhao 2017년 9월 1일
Thank you, this works. And because Button was only a temp variable that I used for the loop, that's why I was overwriting it.
Adam
Adam 2017년 9월 1일
loop variables are usually temporary, but you don't overwrite them in the loop.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by