Katoch
Katoch

Reputation: 2777

create variables in base workspace - from function calling script

In my loadsignals.m script has a function which runs a script test.m to create variables in base workspace.

Why variables are not created in base workspace ?

How can i create variables in base workspace I do not want to use assignin function ?

loadsignals.m :--

function loadSignals(VarName)

  ... do some work ...

  run(test);

end

test.m :--

a = uint8(10);
b = uint8(20);
c = uint16(0); 

Upvotes: 0

Views: 620

Answers (1)

Matt
Matt

Reputation: 13923

When you call a script from a function, the script uses the function workspace. Therefore the created variables are not stored in the base workspace unless you do it explicitly. This can be done with assignin as already mentioned.

One reason of not using assignin could be that you don't want to modify the script test.m itself. To circumvent this, you can use evalin to execute test.m in the base workspace. The variables are then getting stored in the base workspace as well.

evalin('base','run(''test.m'')');

Note that run(test) may not work because the file test.m is a script and not a function. You can use run('test.m') instead. To have the ' in a string, you need to write it twice '' like shown in the second argument of evalin.

Upvotes: 2

Related Questions