이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Error "Undefined function or variable"
조회 수: 2 (최근 30일)
이전 댓글 표시
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일
Hey I tried what you mentioned above,
[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
But it still shows undefined function or variable 'hcEqPt'.
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.
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일
Relax man, I'm quite new to this and trying to learn.
Also, I did remove the function from the beginning of the file and ran the code. But it still shows the error I mentioned above.
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).
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
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일
To be honest, I had not defined roots anywhere before in the file. When I remove it, the error moves to the next line and happens subsequently.
![Screen Shot 2019-09-08 at 9.32.47 PM.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/237507/Screen%20Shot%202019-09-08%20at%209.32.47%20PM.png)
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)
madhan ravi
2019년 9월 9일
Really? Comeon, that's a pdf file not a script file attach your file with .m extension.
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일
Thank you very much for your time. I am quite new to this and trying to cope up with it.
Thank you for being patient. :)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)