CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum by 3S-Smart Software Solutions GmbH | A member of the CODESYS Group
Deutsche Version English version russian version 
It is currently Fri Dec 06, 2019 1:35 am

All times are UTC+01:00




Post new topic  Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Wed Feb 20, 2019 6:39 pm 
Offline

Joined: Thu Dec 06, 2018 2:48 pm
Posts: 7
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);


Top
   
PostPosted: Thu Feb 21, 2019 1:30 am 
Offline
Frequent User
Frequent User

Joined: Fri Sep 02, 2011 8:02 pm
Posts: 309
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!!!

_________________
Scott Cunningham
KEB America, Inc.
www.kebblog.com
www.kebamerica.com


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 2 posts ] 

All times are UTC+01:00


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited