Why is MATLAB code excution in a function not in a sequence like in the main script?

So here is the example:
% I run the initparameter file first
% the file contains a rigidbodytree object called robot
% Then define results = robot, it works fine
run('InitParameters.m');
results=robot;
% However, if I put this into a function
% The 'initparameter' file is not excuted first.
% This gives me an error indicating that robot is not found.
% The 'initparameter' file is not executed at all.
result=myfun();
function results=myfun()
run('InitParameters.m');
results=robot;
end
So is there a way to have the code run in sequence in a function just like in the main script? Maybe there is a duplicate to this question, but I can't seem to describe this properly... Please excuse me.

7 Comments

Hi,
Is your function file in the same path with your InitParameters.m?
Best solution is to specific the full path name for InitParameters.m
run('C:\Users\7.0 Community Question\14 reshape\InitParameters.m')
Hi, thanks for the reply.
The function is defined in the main script.
I tried changing to full path, but it doesn't seem to work, the error message isn't 'not able to find the file'.
Instead, the code seems to skip the 'run' command, and executed 'results=robot' straight away. Reason is that my workspace doesn't have all the parameters generated by the 'initparameters' file at all.
I suspect MATLAB function doesn't execute codes line by line, which might be something I don't know of.
InitParameters.m might be deliberately assigning values in the 'base' workspace.
@Walter Roberson
Yes, that's my intention to assign the parameters in that file to the base workspace.
The question is, whether the run command is directly written in a script, or in a function, that makes a difference.
Written in the main script, it can run first then use the values in base workspace generated by the file to define the next variable.
However, if written in a function, it seems that the second line (results=robot) is run before the first line (run command).
Hope that clarifies.
First, to clarify your doubt about MATLAB function doesn't execute codes line by line
It is not possible. It is fundamental of programming to execute line by line. No doubt on that.
Use run to run script in function
It is very interesting to know that this way is not working. It is also my first time to see it.
Why?
For sure, it will run the script, but it is not interact with your function and also your MATLAB.
As you mentioned, there are not enough parameters, therefore it should return error in your MATLAB. But it did't. Interesting, it is run but not interact with your MATLAB and your function.
Any variable in the script won't be stored in your workspace and within your function. There is no interaction. I don't have good explanation for it, probably we may need to seek advice from MathWorks or expert here.
How to solve it? Don't use run. Just call the script as what majority people will do.
result=myfun();
function results=myfun()
InitParameters;
results=robot;
end
@Kevin, Unfortunately, the same error still occurs.
However, you mentioned 'interaction' which enlightened me. I tried inserting a break point at 'result=robot'. The first line (InitParameter) does execute. However, as soon as I jump to the second line, all the 'base' workspace variables are cleared. So I think it's more of a function scope problem.
That may be also why when the two lines are excuted in the global scope, it doesn't have that issue.
I'll try again with some other methods. Thanks for the advice anyway!
How about this?
result=myfun();
function results=myfun()
robot = [];
InitParameters;
results=robot;
end

Sign in to comment.

 Accepted Answer

run() is a function that determines which file is being invoked and then does an evalin('caller') of the file.
When the script being executed is executed from the command line, the 'caller' will be the base workspace, and any assignin('base') that are executed will result in variables that are directly available to the calling environment because the calling environment is also the base workspace.
When the script being executed is executed from a function, the 'caller' will be the function that run was called from, and any assignin('base') that are executed would result in variables that are in the base workspace but not in the workspace of the function.
The easiest fix for this direct issue would be
function results=myfun()
InitParameters;
results = evalin('base', 'robot');
end

4 Comments

Hi Walter,
I have tried to create a script (a.m) :
c=2
In my main script,
k
function result = k
a;
results = evalin('base', 'c');
end
An eror will be occured at the line of evalin, undefined variable 'c', then I checked the workspace of MATLAB, it don't have 'c'. But if I change my code to
function result = k
a;
results = c;
end
then it will be working fine to return the results. if we use run() to execute the script,
function result = k
run('a.m');
results = c;
end
then it will return undefined variable 'c'. Not sure why does it happen in this way.
Hi @Walter @Kevin,
Thanks for the reply. Now the concept of scope is understood by me. And Kevin's example is right, I tried it and get the same results.
@Kevin, the 'run' command is to create the variables into the base workspace. So if you use 'run', you need to use evalin('base',...). While not using 'run', directly calling k is to create variables into the function local workspace. Then you can use that variable in the function directly.
So basically case is closed. However, I found another tricky discovery from this question:
I found that this still doesn't work for my 'robot' rigidbodytree variable (but other variables do work). Error message prompting 'Attempt to execute SCRIPT robot as a function'. So basically the name 'robot' might have clashed with one of the keywords (but I'm not sure why only clash in this case but not in others). Anyway, I change the variable name from 'robot' to 'robot1' and it works now.
Run does itself deliberately assign values in the base workspace, but the script being executed might.

Sign in to comment.

More Answers (0)

Products

Release

R2018b

Community Treasure Hunt

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

Start Hunting!