Merge multiple functions+scripts in single file

22 visualizzazioni (ultimi 30 giorni)
Jean
Jean il 21 Feb 2012
Modificato: John Kelly il 26 Feb 2015
Hi,
I am trying to group together 4 functions and 2 script files in the same file. Let's call the functions A,B,C and D and the scripts f and g. The way I want this "one-file program" to work is the following:
1) Script f calls function A.
2) Function A calls function C.
-----------------------------
3) Script g calls 2 functions: Function A and D.
4) Function A calls function B.
5) Function D calls function B.
What I want is a file which contains and does all of the above. I managed to make a function which contains functions A and D, but I can't add the others since they are called by functions A and D. I've also been doing some research on "classes" which I've never used before and don't know anything about, but from reading I'm not sure classes will do the trick (I tried them without success, but didn't try very hard)
All I want to know is if this doable or is it too complicated? And if it is doable, what should I use ? Is it classes or something else ?
Thanks a lot to anyone who helps, I've really been working very hard on this and can't seem to make the program work.

Risposte (2)

Honglei Chen
Honglei Chen il 21 Feb 2012
Modificato: John Kelly il 26 Feb 2015
You can only declare subfunctions within a function so unless you are willing to rewrite your scripts into functions, you may not be able to achieve what you want.
Some relevant information:

Jean
Jean il 21 Feb 2012
I tried to do it with subfunctions but it didn't work because of the very intimate relations between the functions. What I tried was the following:
1- Declare all functions as subfunctions.
2- Write the code of the scripts.
So now I have one file with everything in it. However, when functions A and D call functions B and C, they use their names as strings. For example: A('B',other inputs) and A('C',other inputs).
So when I declare the function A as a subfunction, let's call it sub.A, I can call the functions sub.A('B') and it works. But it doesn't for sub.A('sub.B')
Thanks a lot for your time and help! It's very nice of you.
  2 Commenti
Honglei Chen
Honglei Chen il 21 Feb 2012
I couldn't comment much as I don't see the error. However, using function name in string is always tricky. I would suggest to give function handle a try.
http://www.mathworks.com/help/techdoc/matlab_prog/f2-38133.html
Jean
Jean il 22 Feb 2012
Thanks a lot, I'll take a look at it and try to make it work.
I'll get back to you if I have any further problems.
Thanks again, it's very appreciated !

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by