Optimze code with repetive variable numbering

조회 수: 4 (최근 30일)
Peter
Peter 2015년 8월 11일
편집: Matt J 2015년 8월 11일
I use the ui way to generate my graphical user interfaces (suppose to analyze 1 up to 5 traces, depending on the input). There are a couple of radio buttons and if selected I collect the values in a preallocated array. This generates a lot of subfunctions, all with the same logic, e.g. for trigger selection the values are collected in an preallocated array TriggerTrace, if the first trigger (Trigger1) is selectd the first value of the array is set to 1 (see code snippet below), but this logic is also true for trigger2-5. Is there a way to optimize this code and avoid the copy and paste of five seperate subfunctions? Thanks a lot for your advice!
function Trigger1(source,eventdata)
if get(T1_t0,'Value')==1;
TriggerTrace(size(TriggerTrace))=0;
TriggerTrace(1)=1;
if NumberOfTraces > 1; set(T2_t0,'Value',0); end;
if NumberOfTraces > 2; set(T3_t0,'Value',0); end;
if NumberOfTraces > 3; set(T4_t0,'Value',0); end;
if NumberOfTraces > 4; set(T5_t0,'Value',0); end;
end
end

답변 (1개)

Matt J
Matt J 2015년 8월 11일
편집: Matt J 2015년 8월 11일
Why not create a common function called by each of your radio buttons, one which takes the trigger number as the third argument.
function Trigger(source,eventdata,triggerNum)
....
end
  댓글 수: 1
Matt J
Matt J 2015년 8월 11일
Incidentally, this kind of thing
if NumberOfTraces > 1; set(T2_t0,'Value',0); end;
if NumberOfTraces > 2; set(T3_t0,'Value',0); end;
if NumberOfTraces > 3; set(T4_t0,'Value',0); end;
if NumberOfTraces > 4; set(T5_t0,'Value',0); end;
looks like it can be shortened by having a vector of handles T_t0(i) instead of separate ones,
idx=NumberofTraces>(1:4);
set(T_t0(idx), 'Value',0);

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

카테고리

Help CenterFile Exchange에서 Modeling에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by