How to call a function file into a script file?

In my project i've to extract the values of some variables from different function files of MATPOWER. I need to calculate some values using these variables in a script file for simplicity. I have tried [out1,out2,..]=fhandle(in1,in2,...) and @function name to access the functions inside the script file.But it is showing error that the input variable is undefined. Please provide me a solution as soon as possible.

Réponses (1)

Just turn your script into a function by adding one line at the top, and you can get it all done in a single m-file. For example in test.m:
function test
a = 42;
out = MyFunction(a)
function result = MyFunction(inputValue)
result = inputValue * 100;
The "function test" line was all you had to add to the existing script.

6 commentaires

Maha
Maha le 22 Sep 2013
The problem is that all the inputs i need for calculation are available only in certain function files.When i try to call that functions inorder to get the inputs there is an error.i've tried your answer.still its error
Maha
Maha le 22 Sep 2013
function [f, df, d2f] = opf_costfcn(x, om, varargin) this is the function i need to call in a separate mfile to get the value of 'f'
How are you calling it? You can attach your file now, you know. Use the paper clip icon.
Maha
Maha le 22 Sep 2013
i've used the syntax available in mathworks.com.The error is 'x' is an undefined variable
Maha
Maha le 22 Sep 2013
Now only i'm learning MATLAB coding.So i might have asked a silly question without understanding your answer.Please forgive me if so and thanks for replying Sir/Ma'am!
Well you didn't define x before you passed it to a function. How do you think it's supposed to know what x is if you never told it???

Connectez-vous pour commenter.

Catégories

En savoir plus sur Software Development dans Centre d'aide et File Exchange

Question posée :

le 22 Sep 2013

Community Treasure Hunt

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

Start Hunting!

Translated by