Function call doesn't work in script, works in command window
32 views (last 30 days)
Show older comments
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
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?
Answers (2)
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
matthew
on 17 Jul 2018
3 Comments
Stephen23
on 18 Jul 2018
Edited: Stephen23
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.
See Also
Categories
Find more on Whos 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!