[QA]How to load parameter in *.m file by matlab script into workspace

Hello Everyone,
I have a ***.m file (Parameter.m).
In the Parameter.m file. I defined CONST_PARAM = 5;
And I had other script file: CallParam()
function CallParam()
run('Parameter.m')
end
But when I run the function CallParam, the parameter CONST_PARAM did not be included into workspace.
The other hand, if you have a other suggestion please tell me.
Please help me.... I am trouble with this problem.
Thank you very much.

2 Comments

Why do you have the need to create CallParam() function in the first place? , why not directly run the script using run()?
Because, currently I am wring a tool and it will call a lot of different Parameter files for different condition. And above is the problem I would to help.

Sign in to comment.

 Accepted Answer

The function callParam doesn't have any output variable. Then you need something like
function CONST_PARAM = CallParam
run('Parameter.m')
end
And then call the function:
CONST_PARAM = CallParam;

5 Comments

Thank Alex Mcaulley,
But maybe, you have other thinking for my question.
I would to explain for more details:
1/ I have a Parameter.m file (as attached)
2/ I also have a other CallParam.m (as attached)
In command window I typed:
>> CallParam
And my expectation is : CONST_PARAM = 5 in the Workspace
But, Currently It did not as my expectation (there is nothing parameter in workspace)
@ As you suggestion :
function CONST_PARAM = CallParam
run('Parameter.m')
end
This function is not executed because there is no value to return;
Thank you.
The variable CONST_PARAM is loaded in the workspace of your function CallParam, but if you don't specify the output as I suggested this variable dissappears when function CallParam finishes.
Then, you have two options:
My previous suggestion:
function CONST_PARAM = CallParam
run('Parameter.m')
end
Transform CallParam to script:
%Script CallParam
run('Parameter.m')
Hello Alex Mcaulley,
Thank you very much.As so far, I understood your suggestion.
Could I have futher question:
If in the file Parameter.m, I have a lot of Parameter like :
CONST_PARAM = 5;
CONST_PAR = 1;
.............................
CONST_PAMJF = 2;
CONST_PARIEJFI = 3;
In this case, I can not return alot of parameter.
So, how can I do it ?
Thank you very much.
I don't understand why you are using an script to load variables. There are better options like save them in a mat file and then load them in your CallParam function.
By the way, if you still want to do it in the same way, one option is to define all the variables in a struct (data):
data.CONST_PARAM = 5;
data.CONST_PAR = 1;
data.CONST_PAMJF = 2;
data.CONST_PARIEJFI = 3; %and so on
And then, in you function:
function data = CallParam
run('Parameter.m')
end
Thank you very much Alex Mcaulley.
Your answer solved my trouble.
@Because this function for testing automatically.
each parameter file is load, test result is different.
By the way, thank again.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!