Matlab返回多个值以及转化为C/C++工程

目录

1 matlab脚本  StrPrint.m

2  生成c/c++代码

新建 C++工程

调用测试


1 matlab脚本  StrPrint.m

function [sum, mult, strs] = StrPrint(m, n)
strs = 'Happiness is not by chance, but by choice.';
sum = m + n;
mult = m*n;

2  生成c/c++代码

 

下一步直接过点next

 

新建 C++工程

添加现有项把相关的都先加进来,让程序先运行

在StrPrint.cpp中添加相应的头文件和主函数,运行结果如下

调用测试

/*
 * StrPrint.cpp
 *
 * Code generation for function 'StrPrint'
 *
 */

/* Include files */
#include "rt_nonfinite.h"
#include "StrPrint.h"
#include <iostream>
/* Function Definitions */
//测试用二级指针返回值
void StrPrintPointer( const char** str)
{
  int i0;
  static const char cv0[42] = { 'H', 'a', 'p', 'p', 'i', 'n', 'e', 's', 's', ' ',
    'i', 's', ' ', 'n', 'o', 't', ' ', 'b', 'y', ' ', 'c', 'h', 'a', 'n', 'c',
    'e', ',', ' ', 'b', 'u', 't', ' ', 'b', 'y', ' ', 'c', 'h', 'o', 'i', 'c',
    'e', '.' };
  
  *str = cv0;

}


void StrPrint(int m, int n, int *sum, int *mult, char strs[42])
{
	int i0;
	static const char cv0[42] = { 'H', 'a', 'p', 'p', 'i', 'n', 'e', 's', 's', ' ',
		'i', 's', ' ', 'n', 'o', 't', ' ', 'b', 'y', ' ', 'c', 'h', 'a', 'n', 'c',
		'e', ',', ' ', 'b', 'u', 't', ' ', 'b', 'y', ' ', 'c', 'h', 'o', 'i', 'c',
		'e', '.' };


	long long i1;
	for (i0 = 0; i0 < 42; i0++) {
		strs[i0] = cv0[i0];
	}



	if ((m < 0) && (n < MIN_int32_T - m)) {
		*sum = MIN_int32_T;
	}
	else if ((m > 0) && (n > MAX_int32_T - m)) {
		*sum = MAX_int32_T;
	}
	else {
		*sum = m + n;
	}

	i1 = (long long)m * n;
	if (i1 > 2147483647LL) {
		i1 = 2147483647LL;
	}
	else {
		if (i1 < -2147483648LL) {
			i1 = -2147483648LL;
		}
	}

	*mult = (int)i1;
}

/* End of code generation (StrPrint.cpp) */

void main()
{
	int m = 10;
	int n = 100;
	int sum = 0;
	int mult = 0;
	char strs[50] = { 0 };
	char* pstr = new char[42];//最后应该处理为‘\0’,现在没管有错误
	 const char* pstrs = NULL;

	//StrPrint(int m, int n, int *sum, int *mult, char strs[42])
	 StrPrint(m, n, &sum, &mult, strs);
	 StrPrint(1, 20, &sum, &mult, pstr);

	
	std::cout << "sum is " << sum << std::endl
		<< "mult is " << mult << std::endl
		<< "strs is " << std::endl << strs << std::endl;

	std::cout << pstr << std::endl;

	std::cout << "-------------------------------" << std::endl;
	StrPrintPointer(&pstrs);
	std::cout << pstrs<< std::endl;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值