How to pass variables from a matlab file to function in another file

26 次查看(过去 30 天)
I have made a Matlab file SystemDetail.m where I have taken some data as input from the user. I have taken two matrices n, d and a constant o as input from the user in SystemDetail.m. I have another matlab function file GenAlgo.m. I want to use the value of n, d and o as an argument of a function in this function file GenAlgo.m. How should I do it? I have written function sysr = SystemDetail(n,d,o) in my file GenAlgo.m where n d and o are taken from user in SystemDetail.m I am getting the error ' Not enough input arguments. Please tell Where am I wrong?

采纳的回答

Image Analyst
Image Analyst 2017-2-7
You could pass the values into the other function as input arguments, or you could pass in just the filename and let the other function read in the mat file itself. Or, if it's a constant, known filename, you wouldn't even have to pass in the filename, it will just know what it is because you hard coded it in.
  6 个评论
Image Analyst
Image Analyst 2017-2-10
You can't define SystemDetail in both m-files. It's defined in SystemDetail.s do do not define it again in GenAlgo.m. You need to CALL it in GenAlgo.m. So don't have the function keyword before systr. Also, as we can see from SystemDetail.m, SystemDetail() does not even return a value so you can't take the resul and put it into sysr. Finally, SystemDetail does not use the values of n, d, and order that you pass it - it immediately overwrites them with the results of input(). So either put the input lines in GenAlgo.m, or delete the input arguments.
Suggested fix:
SystemDetail.m
function SystemDetail(n,d,order)
g = tf(n,d)
bode(g)
figure
step(g)
% etc. -- other stuff with order or whatever...
GenAlgo.m
n = input('Enter the numerator coefficient from highest power to the lowest in square brackets')
d = input('Enter the denominator coefficient from highest power to the lowest in square brackets')
order = input('Enter the order of the system into which you wish your system be reduced to')
% Call SystemDetail()
SystemDetail(n,d,order);

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by