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?

 採用された回答

Matt J
Matt J 2012 年 11 月 8 日
編集済み: Matt J 2012 年 11 月 8 日

0 投票

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 件のコメント

Jeffrey
Jeffrey 2012 年 11 月 8 日
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 2012 年 11 月 8 日
編集済み: Matt J 2012 年 11 月 8 日
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 2012 年 11 月 8 日
Matt: I was following an example given in the Matlab manual.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangePerformance and Memory についてさらに検索

質問済み:

2012 年 11 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by