R
R
RicardoGonsales2017-01-11 22:47:55
.NET
RicardoGonsales, 2017-01-11 22:47:55

How to translate code from Matlab to C#?

Hello!
There is one rather large project in MATLAB, which it is desirable to translate into C #.
The project is written in classes, the code example is below, the code itself is made similar to sharp.
I understand perfectly well that it will be necessary to arrange types, make some kind of interfaces, that I am unlikely to find automated utilities, and that I will have to cut it with my hands, but the volume scares me - several thousand (xs, 3-4 thousand) lines of code.
Are there at least some syntax converters so as not to change all these function.... end, if... end, for... end to the classic curly braces syntax?
Code example. All code in this OOP style: class-methods-inheritance, there are events

classdef ENode<handle
   
    properties
        nodes_range %Диапазон схемных узлов, соответствующих графическому узлу Зачем нужен?
        connected_elements={};
        is_repacked=false
    end
    
    
    methods
        
        function copy_object=Clone(this)
            constructor=str2func(class(this));
            copy_object=constructor(this.nodes_range);
            %!!! У КОПИИ is_repacked=false!!!
        end
            
        function this=ENode(nodes_range)
             if nargin==1
                 this.nodes_range=nodes_range;                
             end         
        end
        
        function RefreshNodeRange(this,deleted_range) 
           if ~isempty(deleted_range)

                has_selfnode_deleted=false;
                new_range=this.nodes_range;
                        for i=1:length(deleted_range)
                            for j=1:length(this.nodes_range)
                                if deleted_range(i)<this.nodes_range(j)
                                    new_range(j)=new_range(j)-1;
                                elseif deleted_range(i)==this.nodes_range(j) 
                                    new_range(j)=0; %присваивем ему нулевой диапазон
                                    has_selfnode_deleted=true;
                                end
                            end
                        end
                        
                        %На случай, если мы удаляем узел, входящий в
                        %диапазон логического узла
                        if has_selfnode_deleted %если мы удаляли и свои узлы
                            k=1;
                            while 1
                                if new_range(k)==0
                                    new_range=[];
                                else
                                    k=k+1;
                                end
                                if k>length(this.nodes_range)
                                    break
                                end
                            end
                        end
                this.nodes_range=new_range;    
                %2)Диапазон лежит целиком ниже (удаляется нижестоящий) или совпадает (удаляется сам элемент). Тогда ничего не делаем.
           end    
        end

    end
end

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Roman Mirilaczvili, 2017-01-12
@2ord

I don't think this is a good idea. Nothing good usually comes out of code translation.
The advantage of using MatLab is the vectorization of calculations. Without them, the code runs much slower. Judging by the piece of code, it was written as if in an imperative language. If everything is written in this spirit, then perhaps it should be easier to manually translate class by class.
But just in case, I'll give a link: MATLAB Runtime Web Server

R
RicardoGonsales, 2017-01-12
@RicardoGonsales

1) It is C# code that is needed (because then there will be a merger with another project)
2) I am ready to put up with manual rewriting / adding something, the problem is more likely in
MATLAB autocorrect

for i=1:n
...
end

I would like to get automated translation into
C# view
for (i=1; i<n; i++)
{ ... }

And so for all ifs, whiles, etc. There, all the code consists of such things, basically.
Loop within a loop with branching. Matrix mathematics is a minor part there.
Maybe there are similar programs for autocorrect at least for some other languages?

D
Dima Sokolov, 2017-07-18
@dimka11

regexp are not suitable for these purposes?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question