Link to home
Start Free TrialLog in
Avatar of GivenRandy
GivenRandy

asked on

MATLAB, Pass-By-Reference, Redim Preserve

When calling ActiveX Control from MATLAB (v6.5, R13), there seems to be a problem with calling functions that have Redim Preserve in VB. This is not a problem when calling from another VB or LabView app, so I wonder if it is some restriction from MATLAB.

Redim will redimension an array, which is one reason why we need pass-by-value. In some cases, when an array is re-dimensioned, we need to preserve the already existing values. That is where Redim Preserve comes in. While Redim Preserve is required in some situations, it is also 3x to 10x faster in most cases, so that is an additional benefit.

When I disable VB error trapping, so that the error gets returned to MATLAB, I get the following with the only real difference being that the first uses Redim and the second uses Redim Preserve:

---
>> [a,b] = TwoSinglesAsArrayOfTwoSingles(c,single([1,2]))

a =

     1


b =

    3.4000    5.6000

>> [a,b] = TwoSinglesAsArrayOfTwoSinglesPreserve(c,single([1,2]))
??? Invoke Error, Dispatch Exception:
Source: Project1
Description: Subscript out of range.
---

The functions in the ActiveX Control look like this:

---
Public Function TwoSinglesAsArrayOfTwoSingles(ByRef sngTwo() As Single) As Long
    ReDim sngTwo(0 To 1)
   
    sngTwo(0) = 3.4
    sngTwo(1) = 5.6
    TwoSinglesAsArrayOfTwoSingles = 1
End Function

Public Function TwoSinglesAsArrayOfTwoSinglesPreserve(ByRef sngTwo() As Single) As Long
    ReDim Preserve sngTwo(0 To 1)
   
    sngTwo(0) = 34
    sngTwo(1) = 56
    TwoSinglesAsArrayOfTwoSinglesPreserve = 1
End Function
---
Avatar of CyrexCore2k
CyrexCore2k
Flag of United States of America image

Wait... I'm somewhat confused here...

[a,b] = TwoSinglesAsArrayOfTwoSingles(c,single([1,2]))
How is this statement working at all when your function is returning a long NOT an array? That's quite strange.

I'm not familiar with MATLAB but it would seem to me you would have to write your function something like this:

Public Function TwoSinglesAsArrayOfTwoSingles(ByRef sngTwo() As Single) As Single()
   ReDim sngTwo(0 To 1)
   
   sngTwo(0) = 3.4
   sngTwo(1) = 5.6
   TwoSinglesAsArrayOfTwoSingles = sngTwo
End Function




Avatar of GivenRandy
GivenRandy

ASKER

No, you cannot return an array like that in VB6, which is why the reference is passed and you write the values directly. Looks like MathWorks is onto the software defect and may have a patch soon. If so, I will reference it here and PAQ it for reference.
Actually you can return functions like that in VB6. heh... I do it all the time. Perhaps it just doesn't work that way in conjunction with MATLAB
ASKER CERTIFIED SOLUTION
Avatar of GivenRandy
GivenRandy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Please DO NOT DELETE!

The information is useful for future reference. There are already 4 users that I know this impacts on multi-million dollar projects, so this knowledge may prove useful for others. If I get further information in the future, I will make additional comments here.
Thanks!
Got email from Mathworks that they have a fix and they are testing it now. We shall see.