Optimze code with repetive variable numbering
1 回表示 (過去 30 日間)
古いコメントを表示
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
0 件のコメント
回答 (1 件)
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
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 Center および File Exchange で Environment and Settings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!