Plot a function from another .m file in MATLab 2019 - Please help!!!

조회 수: 19 (최근 30일)
Teea Eliade
Teea Eliade 2021년 5월 30일
답변: IONETE Cosmin 2022년 6월 15일
I have this function:
% This function file to be used in conjunction with m5.m
% of Project 5 on permanent magnet drive in Chapter 10.
function y = M5TORQV(sind,Temo,Em,Va,xd,xq)
y = Temo + (Em*Va/xd)*sind + Va*Va*(1/xq - 1/xd)*sind*sqrt(1-sind*sind);
and I call it in another file .m:
clf;
% plots of Figure No.1
subplot(4,1,1)
plot(y(:,1),y(:,2),'-')
ylabel('Tem* in pu')
axis([-inf inf -1.5 1.5])
title('Torque command')
subplot(4,1,2)
plot(y(:,1),y(:,7),'-')
ylabel('Tem in pu')
axis([-inf inf -1.5 1.5])
title('Output torque')
subplot(4,1,3)
plot(y(:,1),y(:,3),'-')
ylabel('Iq* in pu')
axis([-inf inf -1.5 1.5])
title('Rotor reference Iq command')
subplot(4,1,4)
plot(y(:,1),y(:,4),'-')
title('Rotor reference Id command')
ylabel('Id* in pu')
axis([-inf inf -1.5 1.5])
xlabel('Time in sec')
but for some reason, I get this error:
Undefined function or variable 'y'.
Error in m5 (line 281)
plot(y(:,1),y(:,2),'-')
Can you help me, please?
  댓글 수: 3
Teea Eliade
Teea Eliade 2021년 5월 30일
subplot(4,1,1)
plot(y(:,1),y(:,2),'-')
Teea Eliade
Teea Eliade 2021년 5월 30일
편집: Teea Eliade 2021년 5월 30일
the code is not mine, is of professor Chee Mun Ong- I just try to execute it in MATLab 2019 but it is not working
the specific files are m5.m and m5torqv.m

댓글을 달려면 로그인하십시오.

채택된 답변

Steven Lord
Steven Lord 2021년 5월 30일
I have this function:
[snip the text of the function]
and I call it in another file .m:
No, you don't call it in that file, at least based on the excerpt of the code from that file that you posted.
Just because the M5TORQV function happens to contain a variable named y and return it as output does not automatically make references to y in later code call that function and refer to that output argument. Many functions in MATLAB contain as part of their inner workings a variable named y, and some of those return that variable as an output argument.
If you want to call M5TORQV you need to explicitly call it in your other file. Or you need to include some other code that creates the variable y before you try to use it.
  댓글 수: 1
Teea Eliade
Teea Eliade 2021년 5월 30일
Ok, but I still don't understand why this code is not compiling, as it is. I tried to fix it and I thought that there was the problem. If it is not this, I don't know what it is.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

IONETE Cosmin
IONETE Cosmin 2022년 6월 15일
Hi Teea,
Steven's answer is OK.
In defining the function, the output variable 'y' is a formal parameter. The function will need to be called in your MATLAB program as
>> y = M5TORQV (1,1,1,1,1,1);
and then everything should work out.
I don't know the input parameters, I used the value '1' for all parameters as an example, but I'm afraid that the input parameters have vector values since the variable y used for plotting is of matrix type. However, the initial error message should disappear!

카테고리

Help CenterFile Exchange에서 Two y-axis에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by