When do assignin variables become active?

I have a function which contains the following two lines:
import_file(filename); [a b] = filt(f2,f1,Avg);
The import_file is another function which opens up a file and uses the assignin cmd to set variables into the 'base' MathLab workspace. One of these variables is Avg. If I simply manually enter "import_file(filename)" on the CLI line, things work as expected and Avg shows up as an array. However if I run these two lines inside a function, the second command complains that Avg is an Undefined function or variable 'Avg'. It acts like it is hurrying on before Avg actually does get set into the base workspace.
I tried using the waitfor(Avg) command, but that doesn't seem to wait for Avg to appear in the base workspace and then continue.
How do I fix this?

 Respuesta aceptada

Matt J
Matt J el 8 de Nov. de 2012
Editada: Matt J el 8 de Nov. de 2012

0 votos

The base workspace is the workspace of the command line. If you run import_file inside a function, there is no reason to expect Avg to appear in the workspace of that function. It will appear in the workspace of the command line.
Don't use ASSIGNIN. It is an awkward programming tool and has many known dangers...

3 comentarios

Jeffrey
Jeffrey el 8 de Nov. de 2012
The 'base' workspace is the Matlab workspace itself, the 'caller' workspace is that of the caller function. In the import_file() function, the assignin command correctly did set up the variables into the root or base Matlab workspace, which is exactly where it needed to be.
I used an evalin cmd to make a local copy into the function I was using and that has fixed the program. By watching the workspace graphic, I was able to see that the local workspace did NOT contain the Avg variable, nor one other variable that I needed for display graphics later on.
Things are now working as desired and this question is closed.
Matt J
Matt J el 8 de Nov. de 2012
Editada: Matt J el 8 de Nov. de 2012
What you describe is a highly awkward programming approach. Why not just obtain the variables that you want as output arguments of import_file? Then there would be no need to go through the base workspace. If there are many variables, you could pack them into a struct variable.
You also open yourself to a variety of "poofing hazards"
Jeffrey
Jeffrey el 8 de Nov. de 2012
Matt: I was following an example given in the Matlab manual.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Performance and Memory en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 8 de Nov. de 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by