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

7 views (last 30 days)
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 Comments
Teea Eliade
Teea Eliade on 30 May 2021
Edited: Teea Eliade on 30 May 2021
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

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 30 May 2021
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 Comment
Teea Eliade
Teea Eliade on 30 May 2021
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.

Sign in to comment.

More Answers (1)

IONETE Cosmin
IONETE Cosmin on 15 Jun 2022
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!

Categories

Find more on Two y-axis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!