NEED HELP - Cannot call variable from function file back into main script
信息
此问题已关闭。 请重新打开它进行编辑或回答。
显示 更早的评论
I have created a seperate function matlab file to graph some ODEs, I then pick a x value and have it return me the y value of the ODE plot in that function file.
When I go back to the main script where I call my function file It wont let me call any of the variables from the function file. I have tried gloabl variables and everything else I can find on the internet plz help
0 个评论
回答(1 个)
Walter Roberson
2020-10-22
0 个投票
This is correct behaviour for MATLAB. All local variables are discarded when a function returns, with the following exceptions:
- variables declared as "persistent" retain copies between runs of the functions, but it can be difficult to get at those values from outside the function
- variables declared as "global" retain copies that can be accessed from anywhere that declares "global" with the same variable name. Note that declaring a variable global in one place does not make the value accessible everywhere else -- only places that also declare the variable global.
If you have values that you want to be accessible outside the function, you should return their values. Or you should use the debugger to stop in the function before it returns.
3 个评论
Tyler Choate
2020-10-22
Walter Roberson
2020-10-22
Tyler Choate comments to me:
not at all trying to help me solve the issue, why even answer
Walter Roberson
2020-10-22
Tyler, if the methods I discuss are not suitable for your purposes, then the solution is for you to get a job at Mathworks and convince them from inside to undertake a major design change to allow local variables to still be accessible after the function has exited.
It would be a major major design change. One that would break the semantics of all programming languages more advanced than BASIC. It would be very hard to get Mathworks to change this.
I would suggest to you that it would be much easier for you to use one of the strategies that I already posted:
- return the values you need from the function; OR
- put in a breakpoint in the debugger to examine the variables before the function exits; OR
- use one of the several methods discussed in the link to share variables between workspaces.
此问题已关闭。
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!