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!