Tags: calls, function, function-gtsubfunction-gtloop, matlab, programming, subfunction, subfunctioncalls, upto, written

function->subfunction->loop help

On Programmer » Matlab

7,842 words with 9 Comments; publish: Sat, 10 May 2008 00:32:00 GMT; (200234.38, « »)

Hi,

I have written a function that calls a subfunction, and the subfunction

calls another subfunction upto about 9 times.

The reason i have done this is because there are if() statements in

there where one case goes back to a previous subfunction, and the other

case moves on to the next subfunction. in essence, i'm trying to

emulate a JUMP/GOTO type functions in MatLab.

This works reasonably well, but the function keeps looping round

instead of returning a value, which it should do.

function[Rroot Iroot N] = Sii();

% variables set up as global structure so don't need to pass through

each and every function

if condition==0

Fnc2()

end

function Fnc2()

if var.N==1

Fnc9()

end

Fnc11()

function Fnc3()

if var.F>=var.G

Fnc5()

end

if condition

Fnc8();

end

function Fnc5()

if var.M>=20

Fnc6()

end

Fnc3();

function Fnc6()

global var

if (condition)

Fnc8()

end

Fnc9();

function Fnc10()

% do something

Fnc3()

function Fnc11()

% do something

Fnc10()

function[Rroot Iroot N] = Fnc9()

global var % need to make this quit - get's stuck in a loop

var.N=var.Nn;

% this is terminating function - either this of Fnc8

The flow that goes through this function (at the moment) is that it

goes 2->11->10->3->5->3->5 (for 20 times) and then once it has done 20

times, it does fnc6 (which is error message) and should terminate with

subfunction Fnc9

but it doesn't, after Fnc9 it returns to Fnc3 and continues the loop.

how can i stop this, and terminate the overall m-file with the error

message?

Cheers,

Phil

All Comments

Leave a comment...

  • 9 Comments
    • Phil Newman skrev:

      > i'm trying to

      > emulate a JUMP/GOTO type functions in MatLab.

      Why?

      Rune

      #1; Sat, 10 May 2008 00:33:00 GMT
    • Rune Allnor wrote:

      > Phil Newman skrev:

      >

      > Why?

      > Rune

      I'm trying to translate some old PowerBasic code into MatLab, and the

      PB code uses JUMP/GOTO functions, which MatLab doesn't use.

      I want to keep the same routine though.

      Phil

      #2; Sat, 10 May 2008 00:34:00 GMT
    • Phil Newman skrev:

      > Rune Allnor wrote:

      > I'm trying to translate some old PowerBasic code into MatLab, and the

      > PB code uses JUMP/GOTO functions, which MatLab doesn't use.

      > I want to keep the same routine though.

      I suspected something of that kind... have tried it in the past

      with fortran code. My experience is that, unless the program

      is utterly trivial (and I do mean *trivial*) it is almost always faster

      and easier to read up on the routine and implement it from

      scratch in matlab or C. Spaghetty-style coding is very hard

      to translate if you didn't implement the original yourself.

      Rune

      #3; Sat, 10 May 2008 00:36:00 GMT
    • "Phil Newman" <phillenium2002.matlab.todaysummary.com.hotmail.com> writes:

      > Rune Allnor wrote:

      > I'm trying to translate some old PowerBasic code into MatLab, and the

      > PB code uses JUMP/GOTO functions, which MatLab doesn't use.

      > I want to keep the same routine though.

      The language does not have such functionality. Thank goodness. To

      port to new languages, you must use the syntax of the new language.

      In this case, replacing JUMP/GOTO with subfunctions and logical loop

      structures will increase readability and probably performance as well.

      Peter Boettcher <boettcher.matlab.todaysummary.com.ll.mit.edu>

      MIT Lincoln Laboratory

      MATLAB FAQ: http://www.mit.edu/~pwb/cssm/

      #4; Sat, 10 May 2008 00:37:00 GMT
    • Peter Boettcher wrote:

      > "Phil Newman" <phillenium2002.matlab.todaysummary.com.hotmail.com> writes:

      >

      > The language does not have such functionality. Thank goodness. To

      > port to new languages, you must use the syntax of the new language.

      > In this case, replacing JUMP/GOTO with subfunctions and logical loop

      > structures will increase readability and probably performance as well.

      >

      > --

      > Peter Boettcher <boettcher.matlab.todaysummary.com.ll.mit.edu>

      > MIT Lincoln Laboratory

      > MATLAB FAQ: http://www.mit.edu/~pwb/cssm/

      I know that, and that's exactly what I've done! that isn't my problem.

      my problem is described in the first post, in that i get stuck in a

      loop with my sub-functions. how can i avoid this?

      Phil

      #5; Sat, 10 May 2008 00:37:00 GMT
    • Phil Newman skrev:

      > Peter Boettcher wrote:

      > I know that, and that's exactly what I've done!

      Peter was not sufficiently explicit. It is not enough to replace

      JUMP/GOTO with function calls, the structure of the program

      must change as well. I think the term is "structured programming".

      If you can convert the algorithm to a form that comlies with

      "structural programming", it will be a lot easier to implement.

      Rune

      #6; Sat, 10 May 2008 00:39:00 GMT
    • Rune Allnor wrote:

      > Phil Newman skrev:

      >

      > Peter was not sufficiently explicit. It is not enough to replace

      > JUMP/GOTO with function calls, the structure of the program

      > must change as well. I think the term is "structured programming".

      > If you can convert the algorithm to a form that comlies with

      > "structural programming", it will be a lot easier to implement.

      > Rune

      How do you suggest I start?

      Phil

      #7; Sat, 10 May 2008 00:40:00 GMT
    • Phil Newman skrev:

      > Rune Allnor wrote:

      > How do you suggest I start?

      That's the hard part. You may be in for a very big job if you have

      absolutely no idea about what the program does and how it works.

      I assume you don't; otherwise you would not get entangled with

      ancient spaghetti code.

      I suppose you have to start at the beginning. See what the program

      does at first, before any conditionals, jumps, or gotos, and assume

      that is initialization.

      On a conditional, the program either goes on or jumps. Seems to be

      a IF-THEN -ELSE statement. Step into the branch, and start over.

      Start over once more after the jump, when you have finished inside.

      Sounds laborous? Difficult to keep track of what's going on?

      That's because it is. These are the very reasons why this style

      of programming was all but abandoned some 30 years ago.

      Rune

      #8; Sat, 10 May 2008 00:41:00 GMT
    • Rune Allnor wrote:

      > Phil Newman skrev:

      > That's the hard part. You may be in for a very big job if you have

      > absolutely no idea about what the program does and how it works.

      > I assume you don't; otherwise you would not get entangled with

      > ancient spaghetti code.

      > I suppose you have to start at the beginning. See what the program

      > does at first, before any conditionals, jumps, or gotos, and assume

      > that is initialization.

      > On a conditional, the program either goes on or jumps. Seems to be

      > a IF-THEN -ELSE statement. Step into the branch, and start over.

      > Start over once more after the jump, when you have finished inside.

      > Sounds laborous? Difficult to keep track of what's going on?

      > That's because it is. These are the very reasons why this style

      > of programming was all but abandoned some 30 years ago.

      > Rune

      but that's what i'd been doing with the functions, and they jumped

      abuot nicely and did what i wanted them to do for the first 20 times.

      the problem is that after the 20th time, it didn't terminate properly.

      Phil

      #9; Sat, 10 May 2008 00:42:00 GMT