IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

MATLAB Discussion :

Diviser un long code à des fonctions


Sujet :

MATLAB

  1. #1
    Nouveau membre du Club
    Inscrit en
    Novembre 2010
    Messages
    43
    Détails du profil
    Informations forums :
    Inscription : Novembre 2010
    Messages : 43
    Points : 28
    Points
    28
    Par défaut Diviser un long code à des fonctions
    Bonjour les amis,
    J'ai fais un long code qui contient des parties qui se répètent 2 ou 3 fois.
    Mon encadrant m'a demander de structurer un peu mon code en faisant des fonctions et leurs appels dans un programme principale. moi je manipule pas bien Matlab, et surtout les fonction. Alors s'il vous plait si quelqu'un d'entre vous puisse m'aider à faire cette tache . Voici mon code et 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
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
     clear;
    vid = videoinput('winvideo');
    loadVid(vid);
    c=cell(1,50);
    c{1,50}=[];
    for i=1:10
        figure('Toolbar','none','Menubar', 'none','NumberTitle','Off',...
            'Name','Snapshot','Visible','off');
                 sav=getsnapshot(vid);
             imshow(sav);
             vid.FrameGrabInterval =10;
     
             c{1,i}=sav;
             savv=imresize(sav,[800 1280]);
     
            [a b]=generate_skinmap(savv); 
            [row, col]=find(b);
     
    % % %%regionprops
    [L Ne]=bwlabel(b);
    propied=regionprops(L);
     
    %%%%%%%%%%image en niveau de gris
    imgr=rgb2gray(savv);
     
    s=find([propied.Area]>6400);
    for n=1:size(s,2)
     
        %rectangle('Position',propied(s(n)).BoundingBox,'EdgeColor','g','linewidth',2);
        imcrp=imcrop(imgr,(propied(s(n)).BoundingBox));
     
            hold on;
            image=imresize(imcrp,[80 80]);
            %imrot=imrotate(image,180);
            imagei=histeq(image);
     
                 %load reseau90;
                 load rxjdid;
     
            S=[];
            Z=[]; 
            R=[];
     
      cellule=[];
     
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%      
                       S=[imagei(:)];
                       S=double(S);
                       outputs = sim(net,S);
                       R=round(outputs); 
                       x=901/120;
                       y=1201/160;
     
                       if (R==[1;0])
                             disp('d');
     
                             %figure(500);
                              imshow(imgr);
     
                             hold on
                             rectangle('Position',propied(s(n)).BoundingBox,'EdgeColor','g','linewidth',2);
                             p=imcrop(imgr,propied(s(n)).BoundingBox);
                             cellule=propied(s(n)).BoundingBox;
                             xx=cellule(1);
                             yy=cellule(2);
     
                              %[r m]=size(p); 
     
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  SSSSuiviiiiiiiiiiiiiiiiiiiiiiii %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    c1=cell(1,20);
    c1{1,20}=[];
    pause(2);
    ptfixe=cell(2,20);
    ptfixe{2,20}=[];
    ptfixe{1,1}=0;
    ptfixe{2,1}=0;
     
    nbframe=0;
    for k=i+1:20 
        for kk=2:20
                       sav1=getsnapshot(vid);
                       %imshow(sav1);
                       vid.FrameGrabInterval = 5;      
                       savv1=imresize(sav1,[800 1280]);
                        c1{1,k}=savv1;                      
                       %%%%%%%%detection de la couleur de la peau                                    
                                    [a1 b1]=generate_skinmap(savv1); 
                                         [row1, col1]=find(b1);                                     
                                         %%%%%regionprops
                                        [LL Nee]=bwlabel(b1);
                                      propied=regionprops(LL);                                  
                                      %grandim=imread(str1);
                                      gr=rgb2gray(savv1); 
                  si=find([propied.Area]>6400); 
                       ze=size(si,2);
                       cellrect=[];
     
                     Diff=[];
                       souris=[];
    if(ze==1)                        
      rectangle('Position',propied(si(1)).BoundingBox,'EdgeColor','r','linewidth',2);
        souris=propied(si(1)).BoundingBox;
                              xpts=souris(1);
                              ypts=souris(2);
                               indrect=1;
                              set(0,'PointerLocation',[xpts ypts]);                                                 
                                       Diff=[];
                                       ptfixe{1,kk}=xpts;
                                       ptfixe{2,kk}=indrect;
     
    elseif(ze>=2)  
     
         for ni=1:ze
           cellrect=propied(si(ni)).BoundingBox;   
        xpt=cellrect(1);
        ypt=cellrect(2);
        toul=cellrect(3);
        arth=cellrect(4);
        air=toul*arth;
     
         DistEucl=sqrt(((xx-xpt)^2)+((yy-ypt)^2));                                                                    
                          Diff(ni)=DistEucl;
                         [Difsort ind]=sort(Diff);
    end 
     
        rect1=propied(si(ind(1))).BoundingBox;
    rect2=propied(si(ind(2))).BoundingBox;
    air1=rect1(3)*rect1(4);
    air2=rect2(3)*rect2(4);
     get(0,'PointerLocation');
     
                               %figure(500),
                               imshow(gr);
     
    if(air-air1<air-air2)   
     
                      rectangle('Position',propied(si(1)).BoundingBox,'EdgeColor','r','linewidth',2);
                      souris=propied(si(1)).BoundingBox;
                              xpts=souris(1);
                              xx=souris(1);
                              ypts=souris(2);
                               yy=souris(2);
                              indrect=1;
                              set(0,'PointerLocation',[xpts ypts]);                      
                                   Diff=[];
                                   ptfixe{1,kk}=xpts;
                                  ptfixe{2,kk}=indrect;
     
    else
     
          rectangle('Position',propied(si(2)).BoundingBox,'EdgeColor','r','linewidth',2);
          souris=propied(si(2)).BoundingBox;
                              xpts=souris(1);
                              xx=souris(1);
                              ypts=souris(2);
                              yy=souris(2);
                              indrect=2;
                              set(0,'PointerLocation',[xpts ypts]);                      
                                   Diff=[];
                                   ptfixe{1,kk}=xpts;
                                   ptfixe{2,kk}=indrect;
                                   %pause
     
                      end                          
     
    if(abs(ptfixe{1,kk}-ptfixe{1,kk-1})<30)
       nbframe=nbframe+1; 
       if(nbframe>=4)
        disp('rx neuroneeeee');
        nbframe=0;
            SS=[];
            ZZ=[]; 
            RR=[];        
            mainouvrt=[];
            coupe=imcrop(gr,(propied(si(ptfixe{2,kk})).BoundingBox));    
        mainouvrt=(propied(si(ptfixe{2,kk})).BoundingBox);
        xxtr=mainouvrt(1);
         yytr=mainouvrt(2);
           imge=imresize(coupe,[80 80]);
              %imrot1=imrotate(imge,180);
            %figure,imshow(imge);
     
            hist=histeq(imge);
                          load rxcomdjdid97;                              
                             SS=[hist(:)];
                       SS=double(SS);
                       out = sim(net,SS);
                       RR=round(out); 
                       if(RR==[1;0])
                           disp('main-fermée');
                           system('C:\Program Files\Internet Explorer\iexplore.exe');
                       else
                           disp('main-ouverte');                 
                        system('C:\Users\user\AppData\Roaming\Microsoft\Services Windows Live\Raccourci Windows Live Messenger.exe');
                        %pause
     
     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     c1tr=cell(1,40);
    c1tr{1,40}=[];
    pause(2);
    traject=cell(2,20);
                       traject{2,20}=[];
     
                        for ktr=k+1:k+5
     
     
                           savtr=getsnapshot(vid);
                       %imshow(sav1);
                       vid.FrameGrabInterval = 5;      
                       savvtr=imresize(savtr,[800 1280]);
                        c1tr{1,ktr}=savvtr;     
     
                       %%%%%%%%detection de la couleur de la peau                                    
                                    [atr btr]=generate_skinmap(savvtr); 
                                         [rowtr, coltr]=find(btr);                                     
                                         %%%%%regionprops
                                        [LLtr Neetr]=bwlabel(btr);
                                      propied=regionprops(LLtr);                                  
                                      %grandim=imread(str1);
                                      grtr=rgb2gray(savvtr); 
                  sitr=find([propied.Area]>6400); 
                       zetr=size(sitr,2);
                        Difftr=[];
                        souristr=[];
     
     
     % for kktr=ktr+1:ktr+11
     
    if(zetr==1)                        
      rectangle('Position',propied(sitr(1)).BoundingBox,'EdgeColor','r','linewidth',2);
        souristr=propied(sitr(1)).BoundingBox;
    %                           xxtr=souristr(1);
    %                           yytr=souristr(2);
                              traject{1,ktr}=souristr(1);
                             traject{2,ktr}=souristr(2);
                               %indrecttr=1;
                              %set(0,'PointerLocation',[xptstr yptstr]);                                                 
                                       Difftr=[];
    %                                    ptfixe{1,kk}=xpts;
    %                                    ptfixe{2,kk}=indrect;
    %end
     
    elseif(zetr>=2)  
     
         for d=1:zetr
           souristr=propied(sitr(d)).BoundingBox;   
       xpttr=souristr(1);
        ypttr=souristr(2);
        toultr=souristr(3);
        arthtr=souristr(4);
        airtr=toultr*arthtr;
     
         DistEucltr=sqrt(((xxtr-xpttr)^2)+((yytr-ypttr)^2));                                                                    
                          Difftr(d)=DistEucltr;
                         [Difsorttr indtr]=sort(Difftr);
    end
     
        rect1tr=propied(sitr(indtr(1))).BoundingBox;
    rect2tr=propied(sitr(indtr(2))).BoundingBox;
    air1tr=rect1tr(3)*rect1tr(4);
    air2tr=rect2tr(3)*rect2tr(4);
     %get(0,'PointerLocation');
     
                               %figure(500),
                               imshow(gr);
     
    if(airtr-air1tr<airtr-air2tr)   
     
                      rectangle('Position',propied(sitr(1)).BoundingBox,'EdgeColor','r','linewidth',2);
                      souristr=propied(sitr(1)).BoundingBox;
                              %xpts=souris(1);
                              %xxtr=souristr(1);
                              %ypts=souris(2);
                               %yytr=souristr(2);
                              %indrecttr=1;
                              %set(0,'PointerLocation',[xpts ypts]);                      
                                   Difftr=[];
     
                                    traject{1,ktr}=souristr(1);
                                   traject{2,ktr}=souristr(2);
     
     
     
     
    else
     
          rectangle('Position',propied(sitr(2)).BoundingBox,'EdgeColor','r','linewidth',2);
          souristr=propied(sitr(2)).BoundingBox;
          %xpts=souris(1);
                               xxtr=souristr(1);
                              %ypts=souris(2);
                               yytr=souristr(2);
                              %indrecttr=2;
                              %set(0,'PointerLocation',[xpts ypts]);                      
                                   Difftr=[];
                                    traject{1,ktr}=souristr(1);
                                    traject{2,ktr}=souristr(2);
     
     
     
    end 
     
         end
     
     
    %end
     
    % for l=1:5
    %       if(isempty(traject{1,l}))
    %         
    %     ptfixe{1,l}=0;
    %     ptfixe{2,l}=0;
    %       end
    %     end
     
     
     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                        end
     
    trrx=[];
    trry=[];
    jtr=1;
    for itr=1:size(traject,2)
    if (~isempty(traject{1,itr}))
     
    trrx(jtr)=traject{1,itr};
    trry(jtr)=traject{2,itr};
    jtr=jtr+1;
    end
     end
    Cord=[];
    Cord=[trrx';trry'];
     
     end
     
       end
    end
    end
     end
    end
                        % break;                      
                       else                    
                         outputs=[];
     
                            R=[];
                             disp('nd');
                             %figure(500);
                               imshow(imgr); 
                              hold on                     
                       end
    end
    end

  2. #2
    Membre habitué
    Homme Profil pro
    Ingénieur opto-électronique
    Inscrit en
    Avril 2010
    Messages
    129
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur opto-électronique
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Avril 2010
    Messages : 129
    Points : 157
    Points
    157
    Par défaut
    Je n'ai pas regardé ton code, mais le fonctionnement des fonctions est assez simple.

    La création d'une fonction se fait de la manière suivante :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    function [sortie1 sortie2] = nomdetafonction(entrée1, entrée 2)
    %code de ta fonction
    sortie1= %blabla
    sortie2= %blabla
    Tu as le choix entre mettre ta fonction dans ton code principal, ou dans un autre fichier .m. Si tu choisis la seconde solution, le nom de ton fichier .m doit être le même que celui de ta fonction.

    Pour appeler une fonction dans ton programme, il te suffit ensuite d'écrire
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    [sortie1 sortie2]=nomdetafonction(entrée1, entrée2)
    Tu pourrais par exemple créer une fonction pour alléger l'écriture de tes boucles for: à l'intérieur de la boucle, tu appelles simplement une fonction qui réaliserai ce que tu fais actuellement dans la boucle.

    La doc sur function : http://www.mathworks.com/help/techdoc/ref/function.html

Discussions similaires

  1. [code::blocks] Créer une dll avec des fonctions en C
    Par dewey01 dans le forum Code::Blocks
    Réponses: 0
    Dernier message: 29/09/2008, 15h24
  2. Les codes des fonctions matlab
    Par Lost in dans le forum MATLAB
    Réponses: 4
    Dernier message: 23/05/2008, 15h36
  3. Comment éditer du code en fonction des flags de precompilation ?
    Par laurenttlse dans le forum Autres éditeurs
    Réponses: 1
    Dernier message: 23/10/2007, 10h05
  4. Réponses: 5
    Dernier message: 13/03/2007, 23h20
  5. Réponses: 4
    Dernier message: 01/03/2007, 13h19

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo