Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Is there a way to use part of arrays in OSCAT functions?

leandroct
2019-02-20
2019-02-21
  • leandroct - 2019-02-20

    I have a big array where I need to make calculations in parts of it. One of that calculation is a median value. In the OSCAT library there is a median calculation, but it seems only to work if I calculate the median of the whole array. My doubt is::
    - Is there a way to run the median funcion in only part of array? (Less than the full array size)
    - Is there a way to run the median function starting not in the first position of array?

    Thanks!


    Here a test program:


    VAR
    (TAB: ARRAY[1..1200] OF REAL;)
    TAB: ARRAY[1..9] OF REAL;
    Mediana: REAL;
    TAM: INT := 36;
    SIZE_TAB: UINT;
    END_VAR

    TAB[1] := 5; TAB[2] := 40; TAB[3] := 25; TAB[4] := 57; TAB[5] := 70; TAB[6] := 34; TAB[7] := 80; TAB[8] := 50; TAB[9] := 95;

    //SIZE_TAB := SIZEOF(TAB);
    //If TAB size is 1..9, the SIZEOF is 4*9 = 36. Only if I put this value, the instruction works correctly. Then, I ask:
    // - Is there a way to run the funcion in only part of array? (Less than the full array size)
    // - Is there a way to run the function starting not in the first position TAB[1] of array? (i.e. TAB[3])

    Mediana := OSCAT_BASIC._ARRAY_MEDIAN(ADR(TAB) , TAM);

     

    Related

    Talk.ru: 1
    Talk.ru: 2
    Talk.ru: 3
    Talk.ru: 5
    Talk.ru: 7
    Talk.ru: 8

  • Anonymous - 2019-02-21

    Originally created by: scott_cunningham

    In your example, if you only want to work on the elements 3..9, then you must change the size you pass. The function assumes an entry uses 4 bytes, so if you send it 36, it will calculate you have 9 elements!

    For elements 3..9 you would call oacat(ADR(TAB(3)), SIZEOF(REAL)*7)
    (Remember 3..9 is actually 7 elements!

    I would suggest not hard-coding your array size - use the SIZEOF keyword.

    Also, note that the function modifies the array by sorting, so if you send only a partial, only that part will be modified. Be sure this won’t cause any troubles!

    In general, something like this:

    Start := 3;
    End := 6;
    pStart := ADR(MyArray(Start));
    NmBytes := SIZEOF(REAL)*(End-Start+1);
    Ans := ARRAY_MEDIAN(pStart, NmBytes);

    Code not tested!!!

     

Log in to post a comment.