1
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,

2
  • 1
    What do you mean by latest arg changes? Commented Jan 29, 2014 at 9:56
  • What is up with the double ''? Is this supposed to be a cell array of strings? That is very strange Matlab syntax to me. Also you never assign to myFunc so your function will have no output... Commented Jan 29, 2014 at 9:58

2 Answers 2

2

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];
Sign up to request clarification or add additional context in comments.

Comments

1

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 :-)

7 Comments

The find should be obsolete.
Oh, yeah, sure :-) Logical indexing will also do :-)
I would add assert(sum(logi) == 1)
@Edric That depends on what you want to happen if kind is not in the list. Good to consider that, but perhaps it wil already work fine or perhaps you don't need to worry about it.
It is a bit of guesswork, but the asker may be interested in this: [var1 ' ' locationIs ' ' b{logi}]
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.