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

336 views (last 30 days)
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?

Accepted Answer

David Fink
David Fink on 30 Mar 2019
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.

More Answers (2)

Venkatesh Khemlapure
Venkatesh Khemlapure on 24 Nov 2022
#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 on 10 Dec 2022
Edited: Walter Roberson on 10 Dec 2022
#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;
}

Community Treasure Hunt

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

Start Hunting!