Seite 1 von 1

How to convert a Matlab code to Python?

Verfasst: Freitag 25. März 2022, 15:16
von dann90
Hey Leute,

Ich möchte bitte wissen, wie konvertiert man einen Matlab-Code in Python? gibt es Tools oder Website ?
Matlab-Code:

Code: Alles auswählen

    %Informationsbeschaffung - Tables
    mat = load(matFile);
    tableNames = fieldnames(mat);
    tableNum = numel(tableNames);

    % Informationsausgabe
    fprintf('Found %d tables in \"%s\" ...\n', tableNum, matFile);

    % Schaetzung fuer Groesse des Cell-Arrays
    appLineNo = (tableNum + 1) * numel(fieldnames(mat.(tableNames{1})));
    data = cell(appLineNo, 3);
    lineCounter = 0;

    % Loop ueber Tables
    for tabInd = 1:tableNum

        % Informationsbeschaffung Columns
        tableName = tableNames{tabInd};
        table = mat.(tableName);
        colNames = fieldnames(table);
        colNum = numel(colNames);

        % Informationsausgabe
        fprintf('Found %d columns in table \"%s\" ...\n', colNum, tableName);

        % Loop ueber Columns und Daten sammeln
        timeAxis = table.(colNames{1});

        for colInd = 2:colNum

            % Informationsbeschaffung fuer Schreiben
            colName = colNames{colInd};
            column = table.(colName);

            % Pruefung ob Dimensionen stimmen
            if numel(timeAxis) ~= numel(column)
                fprintf(2, 'Jump over \"%s\" due to dimension error ... \n', colName);
                continue;
            end

            % Daten an Sturktur anhaengen
            lineCounter = lineCounter + 1;
            data(lineCounter, :) = {colName, timeAxis, column};

        end

    end

    % Entfernen �bersch�ssiger Eintraege und schreiben
    data = data(1:lineCounter, :);
    
end
Vielen Dank für Ihre Hilfe
Dan

Re: How to convert a Matlab code to Python?

Verfasst: Freitag 25. März 2022, 15:52
von Sirius3
Nein, es gibt kein Wundertool, das von einer Sprache in eine andere übersetzt. Und wenn es das gäbe, wäre das kein lesbares Python.
In Python sind das ungefähr 5 Zeilen Code.

Re: How to convert a Matlab code to Python?

Verfasst: Freitag 25. März 2022, 16:38
von __blackjack__
Also um dann die Frage noch mal konkret zu beantworten: Man lernt Matlab, damit man versteht was der Code macht. Dann lernt man Python und Numpy. Und dann wendet man das gelernte Wissen an um Python-Code zu schreiben, der das gleiche macht wie der Matlab-Code. 🙂