Reputation: 5789
function myFunc = executeCmdByKind(var1,kind)
switch kind
case 'open'
cmdToExecute = [''var1 '','' locationIs '',''clear''];
case 'close'
cmdToExecute = [''var1 '','' locationIs '',''delete''];
case 'move'
cmdToExecute = [''var1 '','' locationIs '',''move''];
end
a = system(cmdToExecute);
end
My question is : is there a better way "optimal" to write this code as I call the same cmdToExecute only the latest arg changes
Thanks,
Upvotes: 1
Views: 139
Reputation: 10176
There are, as always, several ways of doing it. I'd do it like the following:
function myFunc = executeCmdByKind(var1,kind)
a = {'open', 'close', 'move'};
b = {'clear', 'delete', 'move'};
logi = ismember(a, kind);
cmdToExecute = [var1, locationIs, b{logi}];
That's not the nicest code, but it's short, if you want that ;-)
First I look via kind
which index it has in the vector a
and after retreiving the index, I can then select the corresponding element from b
:-)
Edit: Short and it's easily expandable in the future with more elements :-)
Upvotes: 1
Reputation: 45752
Your syntax makes no sense to me, but I'll stick with it. You could streamline your code like this I guess:
function myFunc = executeCmdByKind(var1,kind)
switch kind
case 'open'
last = ''clear'';
case 'close'
last = ''delete'';
case 'move'
last = ''move'';
end
cmdToExecute = [''var1 '','' locationIs '', last];
Upvotes: 2