How to convert a Matlab code to Python?

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
dann90
User
Beiträge: 3
Registriert: Freitag 11. März 2022, 10:30

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
Sirius3
User
Beiträge: 18279
Registriert: Sonntag 21. Oktober 2012, 17:20

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.
Benutzeravatar
__blackjack__
User
Beiträge: 14078
Registriert: Samstag 2. Juni 2018, 10:21
Wohnort: 127.0.0.1
Kontaktdaten:

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. 🙂
“Vir, intelligence has nothing to do with politics!” — Londo Mollari
Antworten