matlab void,C++调用Matlab混合编程

mwArray类函数信息

主要构造函数 // 创建空的Matlab阵列,类型为mxDOUBLE_CLASS

mwArray() : m_pa(0)

{

if (mclGetEmptyArray((void**)&m_pa, mxDOUBLE_CLASS) == MCLCPP_ERR)

mwException::raise_error();

validate();

}

// 创建mxID,指定类型为Matlab阵列

mwArray(mxClassID mxID) : m_pa(0)

{

if (mclGetEmptyArray((void**)&m_pa, mxID) == MCLCPP_ERR)

mwException::raise_error();

validate();

}

// 创建行数为num_rows,列数为num_cols,类型为Matlab阵列,对于数值型阵列,cmplx为带创建真累是否为复数阵列

mwArray(mwSize num_rows, mwSize num_cols, mxClassID mxID, mxComplexity cmplx = mxREAL) : m_pa(0)

{

if (mclGetMatrix((void**)&m_pa, num_rows, num_cols, mxID, cmplx) == MCLCPP_ERR)

mwException::raise_error();

validate();

}

// 创建任意维数的Matlab阵列,维数为num_dims,各维为dims,mxID指定阵列类型,对于数值型阵列,cmplx为带创建真累是否为复数阵列

mwArray(mwSize num_dims, const mwSize* dims, mxClassID mxID, mxComplexity cmplx = mxREAL) : m_pa(0)

{

if (mclGetArray((void**)&m_pa, num_dims, dims, mxID, cmplx) == MCLCPP_ERR)

mwException::raise_error();

validate();

}

// 根据字符串str创建一个新的字符型阵列

mwArray(const char* str) : m_pa(0)

{

if (mclGetString((void**)&m_pa, str) == MCLCPP_ERR)

mwException::raise_error();

validate();

}

// 创建字符型阵列,字符串由str指定

mwArray(mwSize num_strings, const char** str) : m_pa(0)

{

if (mclGetCharMatrixFromStrings((void**)&m_pa, num_strings,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的示例,演示如何使用C语言调用MATLAB模糊逻辑工具箱的代码。 首先,需要安装MATLAB和模糊逻辑工具箱,并确保在MATLAB中已经编写了一个模糊逻辑系统。 然后,可以使用MATLAB编译器将MATLAB代码编译为可在C语言中使用的函数。以下是一个示例MATLAB代码: ``` function output = fuzzyLogic(input1, input2) % Create a fuzzy logic system fis = mamfis('Name','myfis','NumInputs',2); fis = addInput(fis,[-2 2],'Name','input1'); fis = addInput(fis,[-2 2],'Name','input2'); fis = addOutput(fis,[-1 1],'Name','output'); fis = addMF(fis,'input1','trimf',[-2 -1 0],'Name','Negative'); fis = addMF(fis,'input1','trimf',[-1 0 1],'Name','Zero'); fis = addMF(fis,'input1','trimf',[0 1 2],'Name','Positive'); fis = addMF(fis,'input2','trimf',[-2 -1 0],'Name','Negative'); fis = addMF(fis,'input2','trimf',[-1 0 1],'Name','Zero'); fis = addMF(fis,'input2','trimf',[0 1 2],'Name','Positive'); fis = addMF(fis,'output','trimf',[-1 -0.5 0],'Name','Negative'); fis = addMF(fis,'output','trimf',[-0.5 0 0.5],'Name','Zero'); fis = addMF(fis,'output','trimf',[0 0.5 1],'Name','Positive'); ruleList = [1 1 1 1 3; 1 2 2 2 2; 2 1 2 2 1; 2 2 3 3 2; 3 1 3 3 3; 3 2 2 2 2]; fis = addRule(fis,ruleList); % Evaluate the fuzzy logic system output = evalfis([input1 input2],fis); ``` 此代码创建了一个名为“myfis”的模糊逻辑系统,并定义了两个输入(“input1”和“input2”)和一个输出(“output”)。还定义了三个成员函数(“Negative”,“Zero”和“Positive”),它们分别用于表示输入和输出的模糊概念。然后,使用addMF函数将这些成员函数添加到相应的输入和输出中。最后,使用addRule函数添加了一组规则,以定义输入和输出之间的关系。 可以使用以下命令将此代码编译为可在C语言中使用的函数: ``` mcc -m fuzzyLogic.m -a fuzzyLogic.fis ``` 其中,“fuzzyLogic.m”是上面的MATLAB代码的文件名,“fuzzyLogic.fis”是模糊逻辑系统的文件名。这将生成两个文件:fuzzyLogic.exe和fuzzyLogic.ctf。 现在,可以在C语言中调用fuzzyLogic.exe并传递输入参数。以下是一个示例C代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include "mclmcrrt.h" int main(void) { int status; mxArray *input[2], *output[1]; // Initialize the MATLAB runtime if (!mclInitializeApplication(NULL, 0)) { printf("Could not initialize the MATLAB runtime.\n"); return -1; } // Load the compiled MATLAB function status = mclmcrInitialize(); if (status != 0) { printf("Error: %d\n", status); return -1; } // Prepare the input arguments input[0] = mxCreateDoubleScalar(0.5); input[1] = mxCreateDoubleScalar(-0.3); // Call the MATLAB function status = mclFeval("fuzzyLogic", 1, output, 2, input); if (status != 0) { printf("Error: %d\n", status); return -1; } // Get the output value double result = mxGetScalar(output[0]); // Display the result printf("Result: %f\n", result); // Clean up the MATLAB runtime mclTerminateApplication(); return 0; } ``` 此代码使用mclInitializeApplication函数初始化MATLAB运行时,并使用mclmcrInitialize函数加载编译的MATLAB函数。然后,使用mxCreateDoubleScalar函数创建两个输入参数,并使用mclFeval函数调用编译的函数。最后,使用mxGetScalar函数检索输出值,并使用mclTerminateApplication函数清理MATLAB运行时。 注意,此示例代码仅说明如何调用编译的MATLAB函数,而不是如何编写MATLAB代码。如果需要更详细的说明,请参考MATLAB文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值