Error "Undefined function or variable"
显示 更早的评论
I defined a function;
function [maBar, wBar, alphaBar] = hcEqPt(vBar, c1, c2, c3, c4)
wBar = vBar/0.129;
maBar = (36.6 + 0.106*wBar + c4*(wBar^2))/c3;
alphaBar = (acos(1-(c2*wBar*maBar/c1)) + 0.0252)/ 1.14;
end
[maBar_1, wBar_1, alphaBar_1] = hcEqPt(22, 0.6, 0.095, 47500, 0.0026);
disp("At v=22, ma = %d, w = %d, alpha = %d", maBar_1, wBar_1, alphaBar_1);
When I try to use this function to obtain a values of vBar further in the script it shows an error "Undefined function or variable".
Kindly help.
回答(1 个)
madhan ravi
2019-9-9
[maBar_1, wBar_1, alphaBar_1] = hcEqPt(22, 0.6, 0.095, 47500, 0.0026);
fprintf('At v=22, ma = %d, w = %d, alpha = %d', maBar_1, wBar_1, alphaBar_1);
function [maBar, wBar, alphaBar] = hcEqPt(vBar, c1, c2, c3, c4)
wBar = vBar/0.129;
maBar = (36.6 + 0.106*wBar + c4*(wBar^2))/c3;
alphaBar = (acos(1-(c2*wBar*maBar/c1)) + 0.0252)/ 1.14;
end
22 个评论
madhan ravi
2019-9-9
function definition should be at the end of the file when you use it in a script file: https://in.mathworks.com/help/matlab/ref/function.html#description
Shikhar Sharma
2019-9-9
madhan ravi
2019-9-9
编辑:madhan ravi
2019-9-9
Hey what version are you using?? It works for me without a flaw. If you’re using version prior to 2016b save the function in a separate file , the name of the file should be in the name of the function.
Shikhar Sharma
2019-9-9
madhan ravi
2019-9-9
Could you share a screenshot of the script file?
Shikhar Sharma
2019-9-9
madhan ravi
2019-9-9
Man you have some serioud problems in just copying properly. Did you even read the answer properly?? THE FUNCTION DEFINITION SHOULD BE AT THE END OF THE FILE!!!
Shikhar Sharma
2019-9-9
madhan ravi
2019-9-9
编辑:madhan ravi
2019-9-9
Paste the screenshot of the one with the changes you made. Those wiggly orange and red lines are not for decorations hover the mouse over it (it'll display the warning).
Shikhar Sharma
2019-9-9
madhan ravi
2019-9-9
Copy the below and paste it in a new file named Shikhar.m and run the code , let's see if that triggers any errors.
[maBar_1, wBar_1, alphaBar_1] = hcEqPt(22, 0.6, 0.095, 47500, 0.0026);
fprintf('At v=22, ma = %d, w = %d, alpha = %d', maBar_1, wBar_1, alphaBar_1);
function [maBar, wBar, alphaBar] = hcEqPt(vBar, c1, c2, c3, c4)
wBar = vBar/0.129;
maBar = (36.6 + 0.106*wBar + c4*(wBar^2))/c3;
alphaBar = (acos(1-(c2*wBar*maBar/c1)) + 0.0252)/ 1.14;
end
Shikhar Sharma
2019-9-9
madhan ravi
2019-9-9
You can remove that line , sir Walter and I suggested in your previous question is because you had defined roots as a variable somewhere so once you clear it from workspace it would'nt be a problem. You can just hover the mouse to that red line and see what message MATLAB displays there (this is only if you're curious to prevent it in the future.) Once you figure it out please do share the solution.
Shikhar Sharma
2019-9-9
madhan ravi
2019-9-9
编辑:madhan ravi
2019-9-9
https://in.mathworks.com/matlabcentral/answers/479467-error-undefined-function-or-variable#comment_743649 - this is the basic reason causing the problem then, I know you would'nt accept it but the fact remains as is.
As you yourself has mentioned https://in.mathworks.com/matlabcentral/answers/479467-error-undefined-function-or-variable#comment_743665
If you're still not able to cope up with all the mentioned above suggestions , I request you to upload your script file here, so that I can rectify the mistakes if any.
Walter Roberson
2019-9-9
You cannot define the same function twice in one script (except in some cases having to do with nested functions)
Shikhar Sharma
2019-9-9
madhan ravi
2019-9-9
Really? Comeon, that's a pdf file not a script file attach your file with .m extension.
Shikhar Sharma
2019-9-9
Walter Roberson
2019-9-9
All function definitions must be at the end of a script. No exceptions.
Possibly they intend that you use separate .m files for each section.
madhan ravi
2019-9-9
编辑:madhan ravi
2019-9-9
Looks like you didn't give any attention to the suggestion , well done :). See the attached file.
Please again don't miss the valuable comment given by sir Walter
Shikhar Sharma
2019-9-9
类别
在 帮助中心 和 File Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





