Domanda

I have to run a large number of iterations (10^6) for a particular function which calls several other functions. One of the functions it calls which has a switch case statement as there are two parts to the code. I obviously don't want the prompt to come 10^6 times, is there a way for me to only get the prompt to come up in the 1st iteration and to follow through with the rest of the iterations based on that choice? Or is there any other way I could go about this?

Thank You, Jojo

È stato utile?

Soluzione

What you are doing here is probably a design issue that you may be able to resolve differently. Prompting for a parameter/option deep inside the call hierarchy seems wrong. To answer your question anyways, you could make that parameter, let's call it the multiplier f, a persistent variable of that inner function:

function c = dosomething(a, b)
  persistent f;
  while isempty(f)
      f = input('enter multiplier: ');
  end
  c = f * a + b;

In the example, f will be empty [] the first time dosomething is called. In that case, the user will be prompted to enter a value. In subsequent calls, f will still have this value (due to the declarator persistent), and the user will not be prompted again.

A much better solution may be to have the outer function pass the parameter to dosomething(a, b, f);, so the outer function may be the one that prompts the user.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top