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

109 ビュー (過去 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 Center および File ExchangeGet 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