I'm spending quite abit of time cleaning up old MATLAB code - This function attempts to disable Java and uicomponent handles which are properties of some object - any thoughts on how to write this function 'better'? Thanks
function disable(obj)
all_props = properties(obj);
for ii = 1:size(all_props, 1)
开发者_运维技巧 try
set(obj.(all_props{ii}), 'Enabled', 0)
end
try
set(obj.(all_props{ii}), 'Enable', 'Off')
end
end
end
This all looks pretty OK, I'd personally rename the iterator variable 'ii' to something more literal (i.e. iProperty) and at the beginning of the loop, I'd put something like
property = obj.(all_props{iProperty});
to avoid the repeated code afterwards (if your objects allow this).
Instead of 'size(all_props,1)' you could also use 'numel(all_props)'. In this case (with a 1-dimensional array) the outcome is the same and it looks cleaner if you ask me.
If you'd like your code more condense and with less repetition: you can always store 'Enable' and 'Enabled' together with 0 and 'Off' in some array/struct and iterate over these values. It has both its advantages (easily extended to other field names (e.g. 'Disabled' = 1), less code duplication (try-end)) but also disadvantages (extra for loop, extra variables, ...).
精彩评论