How to pass a vector as an argument in vba function generated in MATLAB

6 次查看(过去 30 天)
Hi,
I have the following simplified version of my code:
function [y_hf] =testfunction(y_lf,s)
n_hf=sum(s);
y_hf= [ zeros(n_hf,1) ; y_lf];
And that is what it is supposed to do:
>> y_lf=[1 2 3 4]';
>> s=[1 4 5 7]';
>> y_hf=testfunction(y_lf,s)
y_hf =
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
2
3
4
>>
Using the compiler toolbox I created an excel Add-In for this function.
The only way that I managed to make the function work in Excel VBA is by declaring the input arguments as 2-D arrays with 1 column and variable number of rows, such as the following example:
option explicit
Sub testme()
Dim s(5, 1) As Double
Dim y_lf(5, 1) As Double
Dim y_hf As Variant
s(1, 1) = 3
s(2, 1) = 4
s(3, 1) = 6
s(4, 1) = 3
s(5, 1) = 9
y_lf(1, 1) = 10
y_lf(2, 1) = 20
y_lf(3, 1) = 15
y_lf(4, 1) = 25
y_lf(5, 1) = 18
y_hf = testfunction(y_lf, s)
Range("A1").Resize(UBound(y_hf, 1), 1).Value = y_hf
End Sub
But what I want to have as arguments in the function are vectors, like the following:
Dim s(5) As Double
Dim y_lf(5) As Double
s(1) = 3
s(2) = 4
s(3) = 6
s(4) = 3
s(5) = 9
Dim y_hf As Variant
y_lf(1) = 10
y_lf(2) = 20
y_lf(3) = 15
y_lf(4) = 25
y_lf(5) = 18
This is the error I get as an output in the testFunction output:
Error in testfunction.Class1.1_0: Dimensions of matrices being concatenated are not consistent.
Thank you in advance for your help.
Regards
Thanasis

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Export to MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by