# Unbounded Sized Variables Code Genaration

8 ビュー (過去 30 日間)
Tulio 2014 年 1 月 1 日

Recently i tried to use c-shraed library with c#. as you can see here http://www.mathworks.com/matlabcentral/answers/110992-matlab-codegen-and-cli-clr-class I could figure it out how to call the matlab functions for fixed size variable. However when I try to call it for unbounded sized variables I can´t initialize the variables in the Visual Studio 2012.
This is my code
function [Lambda,V]=My_Eig(cov)
%#codegen
[v,w] = eig(cov);
[Lambda, ind]=sort(diag(w),'descend');
V=v(:,ind);
end
this is my code for fixed size variables in Visual Studio
// This is the main DLL file.
#include "stdafx.h"
#include <array>
#include "Eigenvalue.h"
#include <My_Eig.h>
void Eigenvalue::Class1::Eigen(array<array<double>^>^ input, array<double, 2>^out_Lambda, array<double, 2>^ out_V, int cols)
{
My_Eig_initialize();
const int cov_length = cols * cols;
const int lamda_length = cols;
real_T *Cov = new real_T[cols * cols];
creal_T *Lambda = new creal_T[cols];
creal_T *V = new creal_T[cols * cols];
int c = 0;
// Copy values from input to Cov matrix
for (int i = 0; i < cols; i++)
{
for (int j = 0; j < cols; j++)
{
Cov[c] = input[i][j];
c++;
}
}
// Call My_Eig function to retrieve Lambda and V matrices
My_Eig(Cov, Lambda, V);
// Copy values from Lamda and V to out_Lambda and out_V
for (int i = 0; i < cols; i++)
{
out_Lambda[i,0] = Lambda[i].re;
// out_Lambda[i,1] = Lambda[i].im;
}
for (int i = 0; i < (cols * cols); i++)
{
out_V[i, 0] = V[i].re;
// out_V[i, 1] = V[i].im;
}
My_Eig_terminate();
return;
}
and this is the code generated by matlab
10 #ifndef __MY_EIG_H__
11 #define __MY_EIG_H__
12 /* Include files */
13 #include <math.h>
14 #include <stddef.h>
15 #include <stdlib.h>
16 #include <string.h>
17
18 #include "rtwtypes.h"
19 #include "My_Eig_types.h"
20
21 /* Function Declarations */
22 void My_Eig(const emxArray_real_T *cov, emxArray_creal_T *Lambda, emxArray_creal_T *V);
23 void My_Eig_initialize();
24 void My_Eig_terminate();
25 emxArray_creal_T *emxCreateND_creal_T(int32_T numDimensions, int32_T *size);
26 emxArray_real_T *emxCreateND_real_T(int32_T numDimensions, int32_T *size);
27 emxArray_creal_T *emxCreateWrapperND_creal_T(creal_T *data, int32_T numDimensions, int32_T *size);
28 emxArray_real_T *emxCreateWrapperND_real_T(real_T *data, int32_T numDimensions, int32_T *size);
29 emxArray_creal_T *emxCreateWrapper_creal_T(creal_T *data, int32_T rows, int32_T cols);
30 emxArray_real_T *emxCreateWrapper_real_T(real_T *data, int32_T rows, int32_T cols);
31 emxArray_creal_T *emxCreate_creal_T(int32_T rows, int32_T cols);
32 emxArray_real_T *emxCreate_real_T(int32_T rows, int32_T cols);
33 void emxDestroyArray_creal_T(emxArray_creal_T *emxArray);
34 void emxDestroyArray_real_T(emxArray_real_T *emxArray);
35 #endif
36 /* End of code generation (My_Eig.h) */
% code
How should I tweak the second code to make it work for an unbounded size variable? Thanks In advance
Túlio

サインインしてコメントする。

### 採用された回答

Tulio 2014 年 1 月 2 日
I had a friend that figured it out.
// Initialize Cov Matrix
emxArray_real_T *Cov = emxCreate_real_T(cols, cols);
// Initialize Lambda matrix
emxArray_creal_T *Lambda = emxCreate_creal_T(cols, 1);
// Initialize V matrix
emxArray_creal_T *V = emxCreate_creal_T(cols, cols);
int c = 0;
// Copy values from input to Cov matrix
for (int i = 0; i < cols; i++)
{
for (int j = 0; j < cols; j++)
{
Cov->data[c] = input[i][j];
c++;
}
}
// Call My_Eig function to retrieve Lambda and V matrices
My_Eig(Cov, Lambda, V);
// Copy values from Lambda and V to out_Lambda and out_V
for (int i = 0; i < cols; i++)
{
out_Lambda[i,0] = Lambda->data[i].re;
out_Lambda[i,1] = Lambda->data[i].im;
}
for (int i = 0; i < (cols * cols); i++)
{
out_V[i, 0] = V->data[i].re;
out_V[i, 1] = V->data[i].im;
}
// Before quit the function, release allocated memory
emxDestroyArray_real_T(Cov);
emxDestroyArray_creal_T(Lambda);
emxDestroyArray_creal_T(V);
My_Eig_terminate();
return;
}

サインインしてコメントする。

### カテゴリ

Find more on Get Started with MATLAB Coder in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by