Function call doesn't work in script, works in command window
11 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
OCDER
el 17 de Jul. de 2018
function main()
x1 = 1;
x2 = 2;
x3 = 3;
x4 = 4;
x5 = 5;
x6 = 6;
[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)
a = x1;
b = x2;
c = x3;
d = x4;
e = x5;
f = x6;
g = x1;
h = x2;
end
I don't get any error... Maybe the error isn't here, but in the full code. Can you provide that?
Steven Lord
el 17 de Jul. de 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?
Respuestas (2)
Matt J
el 17 de Jul. de 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 comentarios
OCDER
el 17 de Jul. de 2018
I also suspected this, but sub-function takes precedence over a private function which takes precedence over a local function elsewhere.
If the breakpoint is being used correctly, it should have called the subfunction TimeCalculations()... odd
matthew
el 17 de Jul. de 2018
3 comentarios
OCDER
el 17 de Jul. de 2018
Editada: OCDER
el 17 de Jul. de 2018
Hm... The "Answer" is reinforcing bad coding habits that'll cause more trouble later. Just recount how long it took to debug this error, and no one in the forum could pinpoint the issue immediately because those are the hard bugs to find. Debugging the code requires a variable-by-variable analysis, which is painful...
The real issues are:
- improper usage of eval or load (no idea if you have the right function/variable.)
- poofing in variables (can't check if you have the variable you need, or variable will be overridden by mistake)
- using clear (now you have to keep track of variables to clear/not clear. more bugs)
- improper variable naming practice that likely overrides matlab built-in functions
To fix, start by:
- Avoid eval, load, assignin, evalin, globals
- Make sure every variable is clearly defined in the function
- Avoid clear by having temporary variables defined in function/subfunction that auto clears them when function ends
- Make all your variables start with a capital letter (until you know every function by heart... actually, just use caps on variable's 1st letter. easy fix.)
Info = struct;
which info %matlab function
which Info %your variable
Stephen23
el 18 de Jul. de 2018
Editada: Stephen23
el 18 de Jul. de 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.
Ver también
Categorías
Más información sobre Function Creation en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!