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] = TwoSinglesAsArrayOfTwoSing les(c,sing le([1,2]))
a =
1
b =
3.4000 5.6000
>> [a,b] = TwoSinglesAsArrayOfTwoSing lesPreserv e(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 TwoSinglesAsArrayOfTwoSing les(ByRef sngTwo() As Single) As Long
ReDim sngTwo(0 To 1)
sngTwo(0) = 3.4
sngTwo(1) = 5.6
TwoSinglesAsArrayOfTwoSing les = 1
End Function
Public Function TwoSinglesAsArrayOfTwoSing lesPreserv e(ByRef sngTwo() As Single) As Long
ReDim Preserve sngTwo(0 To 1)
sngTwo(0) = 34
sngTwo(1) = 56
TwoSinglesAsArrayOfTwoSing lesPreserv e = 1
End Function
---
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] = TwoSinglesAsArrayOfTwoSing
a =
1
b =
3.4000 5.6000
>> [a,b] = TwoSinglesAsArrayOfTwoSing
??? Invoke Error, Dispatch Exception:
Source: Project1
Description: Subscript out of range.
---
The functions in the ActiveX Control look like this:
---
Public Function TwoSinglesAsArrayOfTwoSing
ReDim sngTwo(0 To 1)
sngTwo(0) = 3.4
sngTwo(1) = 5.6
TwoSinglesAsArrayOfTwoSing
End Function
Public Function TwoSinglesAsArrayOfTwoSing
ReDim Preserve sngTwo(0 To 1)
sngTwo(0) = 34
sngTwo(1) = 56
TwoSinglesAsArrayOfTwoSing
End Function
---
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
ASKER
Thanks!
ASKER
Got email from Mathworks that they have a fix and they are testing it now. We shall see.
[a,b] = TwoSinglesAsArrayOfTwoSing
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 TwoSinglesAsArrayOfTwoSing
ReDim sngTwo(0 To 1)
sngTwo(0) = 3.4
sngTwo(1) = 5.6
TwoSinglesAsArrayOfTwoSing
End Function