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

Interfaces Graphiques Discussion :

Lancement d'une fonction avec un bouton poussoir [Débutant]


Sujet :

Interfaces Graphiques

  1. #1
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Septembre 2015
    Messages
    20
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : France, Saône et Loire (Bourgogne)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Transports

    Informations forums :
    Inscription : Septembre 2015
    Messages : 20
    Points : 12
    Points
    12
    Par défaut Lancement d'une fonction avec un bouton poussoir
    Bonsoir à tous!

    Je fais mes premiers pas dans la programmation d'interface graphique Matlab, et je suis confronté à un problème.

    Mon but est d'imposer deux valeurs sur deux sliders, et ensuite d'appuyer sur un bouton poussoir qui lance une fonction. Cette fonction reprend les valeurs des 2 sliders pour les traiter et tracer un graphe 2D fonction de ces valeurs.

    Après quelques recherches, j'ai pu voir que l'outil callback était adapté à ce que je cherchais. J'ai programmé manuellement mon interface graphique, et je me suis lancé.


    Voici mon programme:

    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
    fig1=figure;
     
     
    %Affichage Masse bille
    text01 = uicontrol( fig1 , 'style' , 'text' , 'position' , [100,250,170,30] ,...
    'string' , 'Masse bille mb' , 'fontsize' , 15 , 'BackGroundcolor', 'w');
    texte2=uicontrol(fig1,'Style','text','String',0,'Position', [140,220,80,20],'BackGroundcolor','r');
    slid2=uicontrol(fig1,'style','slider','position', [100,200,150,20] , 'Min' , 0 , 'Max' , 1 , ...
    'callback' , 'set(texte2,''String'', get(slid2 , ''value'' ))', 'BackGroundColor', 'r' );
     
     
     
    %Affichage Longueur poutre
    text02 = uicontrol( fig1 , 'style' , 'text' , 'position' , [100,140,170,30] ,...
    'string' , 'Longueur poutre' , 'fontsize' , 15 , 'BackGroundcolor', 'g');
    texte3=uicontrol(fig1,'Style','text','String',0,'Position', [140,110,80,20],'BackGroundcolor','r');
    slid3=uicontrol(fig1,'style','slider','position', [100,90,150,20] , 'Min' , 0 , 'Max' , 1 , ...
    'callback' , 'set(texte3,''String'', get(slid3 , ''value'' ))', 'BackGroundColor', 'r' );
     
     
     
    %Affichage Push Button2   
       bp1= uicontrol ( fig1 , 'style' , 'push' , 'position' , [100, 350, 60, 30 ] ,...
    'string' , 'Début' , 'callback' , @Test2 )
     
     
     
    function Test2 (slid2,slid3)
     
        mb=get(slid2,'value');
        L=get(slid3,'value');
     
     E=linspace(0,100,80); 
     
     i=1:length(E)
     
    %Calcul de F
    F(i)=(E(i)*5*(1+L)*mb);
     
     
    figure
    z1 = subplot ( 'Position' , [ .05 .1 .4 .4 ] ) ;
    plot ( E,F )
    Cependant, le message d'erreur suivant persiste:

    Error in Test2 (line 12)
    F(i)=(E(i)*5*(1+L)*mb);
     
    Error while evaluating uicontrol Callback
    Il semblerait que ma fonction "test2" ne considère pas mes sliders.. Mon problème est sûrement basique, mais je ne vois pas trop comment faire.

    Merci aux personnes qui sauront m'éclairer!

  2. #2
    Rédacteur/Modérateur

    Avatar de Jerome Briot
    Homme Profil pro
    Freelance mécatronique - Conseil, conception et formation
    Inscrit en
    Novembre 2006
    Messages
    20 304
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Freelance mécatronique - Conseil, conception et formation

    Informations forums :
    Inscription : Novembre 2006
    Messages : 20 304
    Points : 52 889
    Points
    52 889
    Par défaut
    Voici une version corrigée :

    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
    function test
     
    fig1 = figure('tag', 'myFigure');
     
    %Affichage Masse bille
    uicontrol(fig1, 'style', 'text', 'position', [100 250 170 30], ...
        'string', 'Masse bille mb', 'fontsize', 15, 'BackGroundcolor', 'w');
     
    handles.texte1 = uicontrol(fig1, 'Style', 'text', 'String', 0, 'Position', [140 220 80 20], ...
        'BackGroundcolor', 'r');
     
    handles.slider1 = uicontrol(fig1, 'style', 'slider', 'position', [100 200 150 20] , ...
        'Min', 0, 'Max', 1, 'callback', @slider1Update, ...
        'BackGroundColor', 'r');
     
     
    %Affichage Longueur poutre
    uicontrol(fig1, 'style', 'text', 'position', [100 140 170 30] ,...
        'string', 'Longueur poutre', 'fontsize', 15, 'BackGroundcolor', 'g');
     
    handles.texte2 = uicontrol(fig1, 'Style', 'text', 'String', 0, 'Position', [140 110 80 20], ...
        'BackGroundcolor', 'r');
     
    handles.slider2 = uicontrol(fig1,'style','slider','position', [100 90 150 20] , ...
        'Min' , 0 , 'Max' , 1 , 'callback' , @slider2Update, ...
        'BackGroundColor', 'r');
     
     
    %Affichage Push Button2
    uicontrol(fig1, 'style', 'push', 'position', [100 350 60 30], ...
        'string', 'Début', 'callback', @test2);
     
    guidata(fig1, handles);
     
    function slider2Update(obj, event)
     
    h = findobj('type', 'figure', 'tag', 'myFigure');
    handles = guidata(h);
     
    set(handles.texte2, 'String', get(handles.slider2, 'value'))
     
    function slider1Update(obj, event)
     
    h = findobj('type', 'figure', 'tag', 'myFigure');
    handles = guidata(h);
     
    set(handles.texte1, 'String', get(handles.slider1, 'value' ))
     
    function test2(obj, event)
     
    h = findobj('type', 'figure', 'tag', 'myFigure');
    handles = guidata(h);
     
    mb = get(handles.slider1, 'value');
    L = get(handles.slider2, 'value');
     
    E = linspace(0,100,80);
     
    i = 1:length(E);
     
    %Calcul de F
    F(i) = (E(i)*5*(1+L)*mb);
     
    figure
    subplot('Position', [0.05 0.1 0.4 0.4]) ;
    plot(E, F)
    La même version optimisée :

    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
    function test
     
    fig1 = figure('tag', 'myFigure');
     
    %Affichage Masse bille
    uicontrol(fig1, 'style', 'text', 'position', [100 250 170 30], ...
        'string', 'Masse bille mb', 'fontsize', 15, 'BackGroundcolor', 'w');
     
    handles.texte(1) = uicontrol(fig1, 'Style', 'text', 'String', 0, 'Position', [140 220 80 20], ...
        'BackGroundcolor', 'r');
     
    handles.slider(1) = uicontrol(fig1, 'style', 'slider', 'position', [100 200 150 20] , ...
        'Min', 0, 'Max', 1, 'callback', {@sliderUpdate, 1}, ...
        'BackGroundColor', 'r');
     
     
    %Affichage Longueur poutre
    uicontrol(fig1, 'style', 'text', 'position', [100 140 170 30] ,...
        'string', 'Longueur poutre', 'fontsize', 15, 'BackGroundcolor', 'g');
     
    handles.texte(2) = uicontrol(fig1, 'Style', 'text', 'String', 0, 'Position', [140 110 80 20], ...
        'BackGroundcolor', 'r');
     
    handles.slider(2) = uicontrol(fig1,'style','slider','position', [100 90 150 20] , ...
        'Min' , 0 , 'Max' , 1 , 'callback' , {@sliderUpdate, 2}, ...
        'BackGroundColor', 'r');
     
     
    %Affichage Push Button2
    uicontrol(fig1, 'style', 'push', 'position', [100 350 60 30], ...
        'string', 'Début', 'callback', @test2);
     
    guidata(fig1, handles);
     
    function sliderUpdate(obj, event, n)
     
    h = findobj('type', 'figure', 'tag', 'myFigure');
    handles = guidata(h);
     
    set(handles.texte(n),'String', get(handles.slider(n), 'value'))
     
    function test2(obj, event)
     
    h = findobj('type', 'figure', 'tag', 'myFigure');
    handles = guidata(h);
     
    mb = get(handles.slider(1), 'value');
    L = get(handles.slider(2), 'value');
     
    E = linspace(0,100,80);
     
    i = 1:length(E);
     
    %Calcul de F
    F(i) = (E(i)*5*(1+L)*mb);
     
    figure
    subplot('Position', [0.05 0.1 0.4 0.4]) ;
    plot(E, F)
    Il est également possible d'utiliser des fonctions imbriquées :

    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
    function test
     
    fig1 = figure;
     
    %Affichage Masse bille
    uicontrol(fig1, 'style', 'text', 'position', [100 250 170 30], ...
        'string', 'Masse bille mb', 'fontsize', 15, 'BackGroundcolor', 'w');
     
    texte1 = uicontrol(fig1, 'Style', 'text', 'String', 0, 'Position', [140 220 80 20], ...
        'BackGroundcolor', 'r');
     
    slider1 = uicontrol(fig1, 'style', 'slider', 'position', [100 200 150 20] , ...
        'Min', 0, 'Max', 1, 'callback', @slider1Update, ...
        'BackGroundColor', 'r' );
     
     
    %Affichage Longueur poutre
    uicontrol(fig1, 'style', 'text', 'position', [100 140 170 30] ,...
        'string', 'Longueur poutre', 'fontsize', 15, 'BackGroundcolor', 'g');
     
    texte2 = uicontrol(fig1, 'Style', 'text', 'String', 0, 'Position', [140 110 80 20], ...
        'BackGroundcolor', 'r');
     
    slider2 = uicontrol(fig1,'style','slider','position', [100 90 150 20] , ...
        'Min' , 0 , 'Max' , 1 , 'callback' , @slider2Update, ...
        'BackGroundColor', 'r');
     
     
    %Affichage Push Button2
    uicontrol(fig1, 'style', 'push', 'position', [100 350 60 30], ...
        'string', 'Début', 'callback', @test2);
     
        function slider2Update(obj, event)
     
            set(texte2, 'String', get(slider2, 'value'))
     
        end
     
        function slider1Update(obj, event)
     
            set(texte1, 'String', get(slider1, 'value'))
     
        end
     
        function test2(obj, event)
     
            mb = get(slider1, 'value');
            L = get(slider2, 'value');
     
            E = linspace(0,100,80);
     
            i = 1:length(E);
     
            %Calcul de F
            F(i) = (E(i)*5*(1+L)*mb);        
     
            figure
            subplot('Position', [0.05 0.1 0.4 0.4]) ;
            plot(E, F)
     
        end
     
    end
    La même version avec la même optimisation :

    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
    function test
     
    fig1 = figure('tag', 'myFigure');
     
    %Affichage Masse bille
    uicontrol(fig1, 'style', 'text', 'position', [100 250 170 30], ...
        'string', 'Masse bille mb', 'fontsize', 15, 'BackGroundcolor', 'w');
     
    handles.texte(1) = uicontrol(fig1, 'Style', 'text', 'String', 0, 'Position', [140 220 80 20], ...
        'BackGroundcolor', 'r');
     
    handles.slider(1) = uicontrol(fig1, 'style', 'slider', 'position', [100 200 150 20] , ...
        'Min', 0, 'Max', 1, 'callback', {@sliderUpdate, 1}, ...
        'BackGroundColor', 'r');
     
     
    %Affichage Longueur poutre
    uicontrol(fig1, 'style', 'text', 'position', [100 140 170 30] ,...
        'string', 'Longueur poutre', 'fontsize', 15, 'BackGroundcolor', 'g');
     
    handles.texte(2) = uicontrol(fig1, 'Style', 'text', 'String', 0, 'Position', [140 110 80 20], ...
        'BackGroundcolor', 'r');
     
    handles.slider(2) = uicontrol(fig1,'style','slider','position', [100 90 150 20] , ...
        'Min' , 0 , 'Max' , 1 , 'callback' , {@sliderUpdate, 2}, ...
        'BackGroundColor', 'r');
     
     
    %Affichage Push Button2
    uicontrol(fig1, 'style', 'push', 'position', [100 350 60 30], ...
        'string', 'Début', 'callback', @test2);
     
    guidata(fig1, handles);
     
    function sliderUpdate(obj, event, n)
     
    h = findobj('type', 'figure', 'tag', 'myFigure');
    handles = guidata(h);
     
    set(handles.texte(n),'String', get(handles.slider(n), 'value'))
     
    function test2(obj, event)
     
    h = findobj('type', 'figure', 'tag', 'myFigure');
    handles = guidata(h);
     
    mb = get(handles.slider(1), 'value');
    L = get(handles.slider(2), 'value');
     
    E = linspace(0,100,80);
     
    i = 1:length(E);
     
    %Calcul de F
    F(i) = (E(i)*5*(1+L)*mb);
     
    figure
    subplot('Position', [0.05 0.1 0.4 0.4]) ;
    plot(E, F)
    Ingénieur indépendant en mécatronique - Conseil, conception et formation
    • Conception mécanique (Autodesk Fusion 360)
    • Impression 3D (Ultimaker)
    • Développement informatique (Python, MATLAB, C)
    • Programmation de microcontrôleur (Microchip PIC, ESP32, Raspberry Pi, Arduino…)

    « J'étais le meilleur ami que le vieux Jim avait au monde. Il fallait choisir. J'ai réfléchi un moment, puis je me suis dit : "Tant pis ! J'irai en enfer" » (Saint Huck)

  3. #3
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Septembre 2015
    Messages
    20
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : France, Saône et Loire (Bourgogne)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Transports

    Informations forums :
    Inscription : Septembre 2015
    Messages : 20
    Points : 12
    Points
    12
    Par défaut
    Bonjour et désolé pour la réponse tardive!

    Merci beaucoup pour votre aide. J'ai fait fonctionné votre code qui marche parfaitement, et qui m'a permis de comprendre comment utiliser les fonctions findobj et guidata!

    Merci encore pour votre aide.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 4
    Dernier message: 09/03/2014, 09h38
  2. declencher une fonction avec un bouton
    Par audran12 dans le forum Balisage (X)HTML et validation W3C
    Réponses: 4
    Dernier message: 30/04/2007, 13h06
  3. Post avec lancement d'une fonction JS
    Par Seth77 dans le forum Général JavaScript
    Réponses: 4
    Dernier message: 05/07/2006, 14h31
  4. Appeler une fonction avec/sans parenthèses
    Par haypo dans le forum Algorithmes et structures de données
    Réponses: 8
    Dernier message: 29/12/2002, 18h48
  5. Une fonction avec des attributs non obligatoires
    Par YanK dans le forum Langage
    Réponses: 5
    Dernier message: 15/11/2002, 13h39

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