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

12 次查看(过去 30 天)
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
编辑: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