Bonjour,

Je suis actuellement étudiant en stage. Une partie de mon stage consiste à convertir des programmes Matlab en Python. Le seul problème c'est que je ne connais absolument pas Python ...
J'ai trois programmes qui me posent vraiment problème, pourriez-vous me donnez une solution de conversion ?
Je vous serais infiniment reconnaissant !! merci d'avance !


Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
function initialisation(s)
 
%-----------PARTIE INITIALISATION MESSAGE BOX------------------------------
%barre de chargement vide pour le commencement
box1=waitbar(0,'0%','Name','initialisation...',...
            'CreateCancelBtn',...
            'setappdata(gcbf,''canceling'',1)');
setappdata(box1,'canceling',0)
steps=2650;%<-- MAGIE: testé plusieur fois en metteant une incrémentation dans le while mais 
            %   la valeur est différente à chaque fois
n=0;
 
% Build the command string
code_ascii_W=hex2dec('57');
code_ascii_R=hex2dec('52');
pos_mem_faible=hex2dec('64');
pos_mem_fort=hex2dec('00');
nb_registre_faible=hex2dec('01');
nb_registre_fort=hex2dec('00');
poids_valeur1_1=hex2dec('01');%  ]
poids_valeur1_2=hex2dec('00');%  } poids de la valeur à
poids_valeur1_3=hex2dec('00');%  } écrire dans le registre
poids_valeur1_4=hex2dec('00');%  ]
 
 
buf=[code_ascii_W,code_ascii_R,pos_mem_faible,pos_mem_fort,nb_registre_faible,...
    nb_registre_fort,poids_valeur1_1,poids_valeur1_2,poids_valeur1_3,poids_valeur1_4];
 
[crc16hi,crc16lo]=CRC16(buf);
 
fwrite(s,[buf,crc16lo,crc16hi]);
 
fread(s,8);
 
%-------------PARTIE LECTURE DE FIN--------------------------
% Read the initilization register
code_ascii_R=hex2dec('52');
code_ascii_D=hex2dec('44');
pos_mem_faible1=hex2dec('64');
pos_mem_fort1=hex2dec('00');
nb_registre_faible1=hex2dec('01');
nb_registre_fort1=hex2dec('00');
buf2=[code_ascii_R,code_ascii_D,pos_mem_faible1,pos_mem_fort1,nb_registre_faible1,...
    nb_registre_fort1];
[crc16hi_2,crc16lo_2]=CRC16(buf2);
 
 
condition=true;%<-----crétaion d'une boucle do while 
while condition
fwrite(s,[buf2,crc16lo_2,crc16hi_2]);
 
pourcent= floor((n/steps)*100);%<----- Pour affichage sur la fenêtre du pourcentage
if pourcent<= 100
    waitbar(n/steps,box1,sprintf('%d%% ',pourcent));%<-- chargement de la barre et maj du texte
else
    waitbar(n/steps,box1,'100% ')
end
if getappdata(box1,'canceling')%<--- bouton cancel de la message box
 
     %----ARRET INITIALISATION EN COUR---   
     poids_valeur2_1=hex2dec('00');%<-- stop initialisation en changeant le poids d'un registre  
     buf=[code_ascii_W,code_ascii_R,pos_mem_faible,pos_mem_fort,nb_registre_faible,...
     nb_registre_fort,poids_valeur2_1,poids_valeur1_2,poids_valeur1_3,poids_valeur1_4];
     [crc16hi,crc16lo]=CRC16(buf);
     fwrite(s,[buf,crc16lo,crc16hi]);%<--- appel un registre pour stoper l'initialisation
     %------------------------------------
 
     delete(box1);%<---- ferme la fenêtre de la message box
     n=0;%<-- remet le pourcentage à 0
 end
n=n+1;% incrémentation du pourcentage
    for i=1:12 %lecture des valeurs
       val=fread(s,1);
 
       if i==7 %<---- seul valeur qui est sensé changer (sauf CRC16)
           if val==1% passage de 0 à 1 pour la valeur du poids faible du mot
               condition =false;%<--- sortie de boucle
               waitbar(n/steps,box1,'terminé');%<-- affiche terminé
               pause(0.2);%<-- pas forcément utile 
               delete(box1);%<-- ferme la message box
           end
       end
    end
 
end
 
end
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function [result] = initialisationtest(s)
result=0;
 
% Creation of the command string
mot_commande=hex2dec('52'); %<--- R
mot_commande2=hex2dec('44'); %<--- D
 
Pos_mem_faible=hex2dec('64');
Pos_mem_fort=hex2dec('00');
 
Registre_faible=hex2dec('01');%<--- poids faible du nombre de registre
Registre_fort=hex2dec('00');%<--- poids fort du nombre de registre
 
% CRC16 computation
buf=[mot_commande,mot_commande2,Pos_mem_faible,Pos_mem_fort,Registre_faible,Registre_fort];
[crc16hi,crc16lo]=CRC16(buf);
 
% Write the command on the hand
fwrite(s,[buf,crc16lo,crc16hi]);
 
% Read the responce: the useful data are contained at sixth position of the
% command string. If the read data is 1, it means that the hand is
% correctly initialised
for i=1:11
    fread(s,1);
    if i==6
        test=fread(s,1);
    end
end
 
if test==1
    result=1;
end
end
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
close all
 
addpath(genpath(pwd));
%addpath('Read');
 
%variables de detection si l'initialisation est faite
d = 0;%<---- main droite
 
%variables vérifiant si les mains sont déja ouvertes
ouvert_droit=0;
ouvert_gauche=0;
 
%Initialisation de chaque Mains
%delete(instrfindall) ;
 
s=serial('COM5');
fclose(s);
delete(s);
clear s;
 
s=serial('COM5');
set(s, 'BaudRate',460800,'DataBits',8,'StopBits',1,...
    'Parity','none','FlowControl','none','TimeOut',1);
fopen(s);
 
d=initialisationtest(s);
%if d=0 the hand is not yet initialized
if d==0 
    initialisation(s);
    d=1;
end
Si vous aviez une solution pour ça je serais ravi !