Optimze code with repetive variable numbering

1 view (last 30 days)
Peter
Peter on 11 Aug 2015
Edited: Matt J on 11 Aug 2015
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

Answers (1)

Matt J
Matt J on 11 Aug 2015
Edited: Matt J on 11 Aug 2015
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 Comment
Matt J
Matt J on 11 Aug 2015
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);

Sign in to comment.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!