Using functions to create a function

7 Ansichten (letzte 30 Tage)
3nore
3nore am 25 Mär. 2013
I want to create 2 functions called ADX and ADXR (ADXR uses ADX to calculate itself). This is the function for ADX-
function [ adx ] = adx( t, p )
adx(i) = [adx(i-t)*(p-t)+DX)]/p
end
Is this the correct way to program the function for ADXR?
function [ adxr ] = adxr( p )
adxr(i) = [adx(i) + adx(i-p)]/2
end
Or does it need the input arguments of adx -"adx(t,p)"? If so, how do I tell matlab I want the previous adx value and not the current value?
function [ adxr ] = adxr( p )
adxr(i) = [adx(t, p) + adx(i-p)(t,p)]/2
end
  2 Kommentare
Walter Roberson
Walter Roberson am 25 Mär. 2013
Do not name a variable the same thing as your function name: there is too much chance of accidentally getting recursion.
Image Analyst
Image Analyst am 25 Mär. 2013
Maybe he's an old Visual Basic programmer, where it does it that way.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Walter Roberson
Walter Roberson am 25 Mär. 2013
In your line
adx(i) = [adx(i-t)*(p-t)+DX)]/p
end
because "i" is not a parameter to the function and is not a variable you initialize, "i" is going to default to sqrt(-1) . If your code managed to get anywhere, it would fail because you cannot index an array at sqrt(-1)

Image Analyst
Image Analyst am 25 Mär. 2013
Maybe something like this:
function adxrOutput = adxr( p, i )
adxrOutput = [adx(i) + adx(i-p)]/2
end
function adxOutput = adx(t, p, i)
adxOutput = [adxOutput(i-t)*(p-t)+DX)]/p
end
I'm not sure I follow what you're trying to do though, and in the adx function, it's still not going to work (even though I made the output variable separate from the function name), but I don't know how to fix it because you haven't said what you want to do.

Kategorien

Mehr zu Language Fundamentals finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by