MATLAB Answers

Function call doesn't work in script, works in command window

105 views (last 30 days)
matthew
matthew on 17 Jul 2018
Edited: Stephen Cobeldick on 18 Jul 2018
I have a function that calls a sub-function within the same file: in main.m:
function main()
...
[a,b,c,d,e,f,g,h]=TimeCalculations(x1,x2,x3,x4,x5,x6);
...
end
function [a,b,c,d,e,f,g,h]=TimeCalculations(x1,x2,x3,x4,x5,x6)
... %calculations for a,b,c,d,e,f,g,h
end
When I run main(), the program errors on the call to TimeCalculations(). The error output is: too many output arguments. However, when I put a breakpoint at this line and then copy the line directly into the command window, I do not receive and error. Has anyone run into a similar scenario and how did they fix it?
  2 Comments
Steven Lord
Steven Lord on 17 Jul 2018
Please show the full text of the error message (everything in red) and show the rest of the code in your main() function.
Does your main() function contain a call to load?

Sign in to comment.

Answers (2)

Matt J
Matt J on 17 Jul 2018
My only theory - you have another version of TimeCalculations which is not a sub-function. When you copy the call to the command window, it is that version which gets called. From the breakpoint, use the debugger controls to step into TimeCalculations() to see exactly where you are and what is getting computed.
  2 Comments
Matt J
Matt J on 17 Jul 2018
it should have called the subfunction TimeCalculations().
Maybe that's where the bug is. Maybe the shadowed version is the one that is working as it should.

Sign in to comment.


matthew
matthew on 17 Jul 2018
I appreciate the feedback. We tracked down the problem. It's odd. In my psuedocode above, x5 is actually a variable called "info." It's a structure that is imported from another file. When calling TimeCalculations(....,info,...) in the script, Matlab must do something, I'm not sure what, but it doesn't pass the data structure "info" in. Instead, it calls the Matlab built-in function, info(). However, when I copy and paste the code into the command window, Matlab passes in the correct structure. Interestingly, I also couldn't assign the info structure to another variable and try to pass that in; that also resulted in an error. In the end, the only solution I found was to execute the code as follows:
eval('NewInfo=info');
clear info
[...]=TimeCalculations(....,NewInfo,...);
Now it works. Why I had to go this route, I don't understand. If anyone knows why Matlab evaluates the line differently depending on if it's in the m-file or the command window, I'd like to know.
THANKS!
  3 Comments
Stephen Cobeldick
Stephen Cobeldick on 18 Jul 2018
"We tracked down the problem. It's odd."
Its not odd at all: the code is simply badly designed because variables magically appear in the workspace. The solution is simple: do NOT make variables magically appear in any workspace. This is a topic that has been discussed many many times before:
"In the end, the only solution I found was to execute the code as follows"
A much better solution would be to avoid eval and "poof"-ing variables into the workspace.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!