How do I make variables that I enter in a script to apply to a second script without need to run or open the second one?

2 views (last 30 days)
Hi,
I would like to enter variables in a VARIABLE_script.m
and once I run this variable_script.m it should
a) write the values in a MAIN_script.m
b) run the MAIN_script.m automatically.
The key intention is to avoid opening the MAIN_script.m
and by entering many variables makes fatal changes in
undesired positions.
The idea with a VARIABLE_script.m should fix this problem.
Below I give a small example to illustrate the problem:
Let the MAIN_SCRIPT.m contain the following commands:
k=1;
l=1;
m=15;
alpha=2;
n=alpha*k;
o=alpha*l;
p=alpha*m;
N=[k:l:m;n:o:p];
M=N';
In the VARIABLE_script.m I would like to enter ONLY the values of
k, l, m and alpha.
Then I need to write some command lines (THIS IS THE QUESTION!!!)
indicating in which script the variable should be written and
and by running VARIABLE_script.m
the Main_script would automatically adsorb the values,
execute the functions and give the output the output (for this very
particular case):
1 2
2 4
3 6
4 8
5 10
6 12
7 14
8 16
9 18
10 20
11 22
12 24
13 26
14 28
15 30
I hope someone can help me with this issue.
Thanks a lot for your attention
Emerson

Accepted Answer

Walter Roberson
Walter Roberson on 15 Apr 2011
Also, in your main script, as long as it remains a script (and not a function), you could code
if ~exist('alpha','variable'); alpha=2; end
and so on for the other variables. Then any variable that already has a value will not have that value overwritten by the main script.

More Answers (1)

Friedrich
Friedrich on 15 Apr 2011
Hi Emerson,
I think the easiest way is to use a function, because they are designed to do this.
function [n, o, p, N, M] = my_func(k,l,m,alpha)
n=alpha*k;
o=alpha*l;
p=alpha*m;
N=[k:l:m;n:o:p];
M=N';
end
And you call it through
[a b c d e] = my_func(1,1,15,2)
Friedrich

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!