Convert C++ Code to Matlab Code with MATLAB 2019a

조회 수: 123 (최근 30일)
Tammy Chang
Tammy Chang 2019년 3월 29일
편집: Walter Roberson 2023년 10월 23일
I have large files that I would like to convert from C++ to Matlab code. Previous responses on MATLAB Answers indicate that this must be done manually, but is this still the case for the most recent version of MATLAB?

채택된 답변

David Fink
David Fink 2019년 3월 30일
If your overall goal is to convert C++ code to MATLAB code, yes, manual conversion is still required in R2019a.
However, if you only need to call the C++ code from MATLAB, simpler solutions are possible (in older releases as well as R2019a):
Option A: create a MEX file via MATLAB Coder (using MATLAB Coder on a simple M file that just calls the C++ function using coder.cinclude and coder.ceval) - see the last comment on a similar question.
Option B: create a MEX file via the MEX command (you will need to write a MEX wrapper for the C++ code). More information can be found on this documentation page.

추가 답변 (3개)

Venkatesh Khemlapure
Venkatesh Khemlapure 2022년 11월 24일
#include <iostream>
using namespace std;
int getMax(int arr[], int n)
{
int mx = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > mx)
mx = arr[i];
return mx;
}
void countSort(int arr[], int n, int exp)
{
int output[n];
int i, count[10] = { 0 };
for (i = 0; i < n; i++)
count[(arr[i] / exp) % 10]++;
for (i = 1; i < 10; i++)
count[i] += count[i - 1];
for (i = n - 1; i >= 0; i--) {
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}
for (i = 0; i < n; i++)
arr[i] = output[i];
}
void radixsort(int arr[], int n)
{
int m = getMax(arr, n);
for (int exp = 1; m / exp > 0; exp *= 10)
countSort(arr, n, exp);
}
void print(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}
int main()
{
int arr[] = { 170, 45, 75, 90, 802, 24, 2, 66 };
int n = sizeof(arr) / sizeof(arr[0]);
radixsort(arr, n);
print(arr, n);
return 0;
}

Asad
Asad 2022년 12월 10일
편집: Walter Roberson 2022년 12월 10일
#include <stdio.h>
int main(int argc, char const *argv[])
{
int M1x, M1y, M2x, M2y;
printf("Enter Matrix 1 x and y with a space in between: ");
scanf("%d %d", &M1x, &M1y);
printf("Enter Matrix 2 x and y with a space in between: ");
scanf("%d %d", &M2x, &M2y);
int M1[M1x][M1y];
int M2[M2x][M2y];
int MatOut[50][50];
for (int x1 = 0; x1 < M1x; x1++)
{
for (int y1 = 0; y1 < M1y; y1++)
{
printf("Enter Matrix 1[%d][%d]: ", x1 + 1, y1 + 1);
scanf("%d", &M1[x1][y1]);
}
}
printf("\nMatrix 1 is:\n");
for (int x1 = 0; x1 < M1x; x1++)
{
for (int y1 = 0; y1 < M1y; y1++)
{
printf("%d ", M1[x1][y1]);
}
printf("\n");
}
printf("\n");
for (int x2 = 0; x2 < M2x; x2++)
{
for (int y2 = 0; y2 < M2y; y2++)
{
printf("Enter Matrix 2[%d][%d]: ", x2 + 1, y2 + 1);
scanf("%d", &M2[x2][y2]);
}
}
printf("\nMatrix 2 is:\n");
for (int x2 = 0; x2 < M2x; x2++)
{
for (int y2 = 0; y2 < M2y; y2++)
{
printf("%d ", M2[x2][y2]);
}
printf("\n");
}
printf("\n");
return 0;
}

Radu-Andrei
Radu-Andrei 2023년 10월 23일
#include<iostream.h>
using namespace std;
int main()
{
int a;
do
{
cout<<"Dati un numar strict pozitiv, a=";
cin>>a;
}
while(a<=0);
cout<<"Avem a="<<a<<endl;
system("pause");
}
  댓글 수: 1
Walter Roberson
Walter Roberson 2023년 10월 23일
편집: Walter Roberson 2023년 10월 23일
a = fix(input("Dati un numar strict pozitiv, a="));
fprintf("Avem a=%d\n", a);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB Coder에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by