[GUI] Basi per la programmazione GUI

di il
10 risposte

[GUI] Basi per la programmazione GUI

Buongiorno a tutti!
E' la prima volta che provo a programmare una GUI su Matlab e sto avendo non pochi problemi.
L'obiettivo e' quello di creare una GUI che mi permetta di visualizzare su un plot l'andamento di una certa funzione dopo aver inserito una serie di dati e scelto la funzione da plottare attraverso i pop-up menu.
Ho fatto l'interfaccia grafica ma ora non so come andare avanti: richiamo funzioni, inserimento valori, visualizzazione grafico.
Grazie per il vostro futuro aiuto...

10 Risposte

  • Re: [GUI] Basi per la programmazione GUI

    Hai provato a dare un'occhiata ai manuali?

    https://www.mathworks.com/help/pdf_doc/matlab/buildgui.pdf
  • Re: [GUI] Basi per la programmazione GUI

    Grazie per la risposta! Ho dato un'occhiata al manuale e ho incominciato a programmare.
    Purtroppo mi da quest'errore: Function definition is misplaced or improperly nested.
    Come posso risolvere?
  • Re: [GUI] Basi per la programmazione GUI

    Se non pubblichi il codice al quale stai lavorando ed il log completo dell'errore è impossibile aiutarti.
  • Re: [GUI] Basi per la programmazione GUI

    Ho cambiato un po' il codice. Ora mi dice che non e' definita la variabile fluid che invece penso di aver definito nella Callback relativa al popup-menu fluid.
    Di seguito il codice:
    function varargout = test2(varargin)
    % TEST2 MATLAB code for test2.fig
    %      TEST2, by itself, creates a new TEST2 or raises the existing
    %      singleton*.
    %
    %      H = TEST2 returns the handle to a new TEST2 or the handle to
    %      the existing singleton*.
    %
    %      TEST2('CALLBACK',hObject,eventData,handles,...) calls the local
    %      function named CALLBACK in TEST2.M with the given input arguments.
    %
    %      TEST2('Property','Value',...) creates a new TEST2 or raises the
    %      existing singleton*.  Starting from the left, property value pairs are
    %      applied to the GUI before test2_OpeningFcn gets called.  An
    %      unrecognized property name or invalid value makes property application
    %      stop.  All inputs are passed to test2_OpeningFcn via varargin.
    %
    %      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
    %      instance to run (singleton)".
    %
    % See also: GUIDE, GUIDATA, GUIHANDLES
    
    % Edit the above text to modify the response to help test2
    
    % Last Modified by GUIDE v2.5 23-Oct-2017 14:31:23
    
    % Begin initialization code - DO NOT EDIT
    gui_Singleton = 1;
    gui_State = struct('gui_Name',       mfilename, ...
                       'gui_Singleton',  gui_Singleton, ...
                       'gui_OpeningFcn', @test2_OpeningFcn, ...
                       'gui_OutputFcn',  @test2_OutputFcn, ...
                       'gui_LayoutFcn',  [] , ...
                       'gui_Callback',   []);
    if nargin && ischar(varargin{1})
        gui_State.gui_Callback = str2func(varargin{1});
    end
    
    if nargout
        [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
    else
        gui_mainfcn(gui_State, varargin{:});
    end
    % End initialization code - DO NOT EDIT
    
    
    % --- Executes just before test2 is made visible.
    function test2_OpeningFcn(hObject, eventdata, handles, varargin)
    % This function has no output args, see OutputFcn.
    % hObject    handle to figure
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    % varargin   command line arguments to test2 (see VARARGIN)
    
    % Choose default command line output for test2
    handles.output = hObject;
    
    % Update handles structure
    guidata(hObject, handles);
    
    % UIWAIT makes test2 wait for user response (see UIRESUME)
    % uiwait(handles.figure1);
    
    
    % --- Outputs from this function are returned to the command line.
    function varargout = test2_OutputFcn(hObject, eventdata, handles) 
    % varargout  cell array for returning output args (see VARARGOUT);
    % hObject    handle to figure
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Get default command line output from handles structure
    varargout{1} = handles.output;
    
    
    % --- Executes on selection change in popupmenu_correlation.
    function popupmenu_correlation_Callback(hObject, eventdata, handles)
    % hObject    handle to popupmenu_correlation (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Hints: contents = cellstr(get(hObject,'String')) returns popupmenu_correlation contents as cell array
    %        contents{get(hObject,'Value')} returns selected item from popupmenu_correlation
    
    % Determine the selected data set.
    str = source.String;
    val = source.Value;
    % Set current data to the selected data set.
    switch str{val};
    case 'Gungor & Winterton.1986' % User selects Gungor & Winterton.1986.
        a=1;
    set(handles.Validation,'Vertical and horizontal flow in tubes and annuli, Water/R11/R22/R113/R114/Ethylene/Glycol, d=2.95-3.2mm, p=0.08-20.6bar');
    case 'Chien et al.2014 ' % User selects Chien et al.2014.
         a=2;
    set(handles.Validation,'Horizontal macro and mini-channels:d=1.5,3.0,6.61,7.49 mm, R410A, G=100-600 kg/m2s, q=10-40 kW/m2, Tsat=5-15 *C, x=0.2-1');
    
    end
    
    % --- Executes during object creation, after setting all properties.
    function popupmenu_correlation_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to popupmenu_correlation (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called
    
    % Hint: popupmenu controls usually have a white background on Windows.
    %       See ISPC and COMPUTER.
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
    
    
    % --- Executes on selection change in popupmenu_fluid.
    function popupmenu_fluid_Callback(hObject, eventdata, handles)
    % hObject    handle to popupmenu_fluid (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Hints: contents = cellstr(get(hObject,'String')) returns popupmenu_fluid contents as cell array
    %        contents{get(hObject,'Value')} returns selected item from popupmenu_fluid
    % Determine the selected data set.
    str = source.String;
    val = source.Value;
    % Set current data to the selected data set.
    switch str{val};
    case 'R134a' % User selects R134a
    fluid='R134a';
    case 'R410A' % User selects R410A.
    fluid='r410a.mix';
    
    end
    
    
    % --- Executes during object creation, after setting all properties.
    function popupmenu_fluid_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to popupmenu_fluid (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called
    
    % Hint: popupmenu controls usually have a white background on Windows.
    %       See ISPC and COMPUTER.
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
    
    
    
    function Tsat_Callback(hObject, eventdata, handles)
    % hObject    handle to Tsat (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Hints: get(hObject,'String') returns contents of Tsat as text
    %        str2double(get(hObject,'String')) returns contents of Tsat as a double
    
    
    
    % --- Executes during object creation, after setting all properties.
    function Tsat_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to Tsat (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called
    
    % Hint: edit controls usually have a white background on Windows.
    %       See ISPC and COMPUTER.
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
    
    
    
    function Psat_Callback(hObject, eventdata, handles)
    % hObject    handle to Psat (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Hints: get(hObject,'String') returns contents of Psat as text
    %        str2double(get(hObject,'String')) returns contents of Psat as a double
    
    
    % --- Executes during object creation, after setting all properties.
    function Psat_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to Psat (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called
    
    % Hint: edit controls usually have a white background on Windows.
    %       See ISPC and COMPUTER.
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
    
    
    
    function G_Callback(hObject, eventdata, handles)
    % hObject    handle to G (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Hints: get(hObject,'String') returns contents of G as text
    %        str2double(get(hObject,'String')) returns contents of G as a double
    
    
    % --- Executes during object creation, after setting all properties.
    function G_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to G (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called
    
    % Hint: edit controls usually have a white background on Windows.
    %       See ISPC and COMPUTER.
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
    
    
    
    function q_Callback(hObject, eventdata, handles)
    % hObject    handle to q (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Hints: get(hObject,'String') returns contents of q as text
    %        str2double(get(hObject,'String')) returns contents of q as a double
    
    
    % --- Executes during object creation, after setting all properties.
    function q_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to q (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called
    
    % Hint: edit controls usually have a white background on Windows.
    %       See ISPC and COMPUTER.
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
    
    
    
    function x_Callback(hObject, eventdata, handles)
    % hObject    handle to x (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Hints: get(hObject,'String') returns contents of x as text
    %        str2double(get(hObject,'String')) returns contents of x as a double
    
    
    
    % --- Executes during object creation, after setting all properties.
    function x_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to x (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called
    
    % Hint: edit controls usually have a white background on Windows.
    %       See ISPC and COMPUTER.
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
    
    
    
    function d_Callback(hObject, eventdata, handles)
    % hObject    handle to d (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Hints: get(hObject,'String') returns contents of d as text
    %        str2double(get(hObject,'String')) returns contents of d as a double
    
    
    % --- Executes during object creation, after setting all properties.
    function d_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to d (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called
    
    % Hint: edit controls usually have a white background on Windows.
    %       See ISPC and COMPUTER.
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
    
    
    
    function c_l_Callback(hObject, eventdata, handles)
    % hObject    handle to c_l (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Hints: get(hObject,'String') returns contents of c_l as text
    %        str2double(get(hObject,'String')) returns contents of c_l as a double
    
    
    % --- Executes during object creation, after setting all properties.
    function c_l_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to c_l (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called
    
    % Hint: edit controls usually have a white background on Windows.
    %       See ISPC and COMPUTER.
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
    
    
    
    function rho_l_Callback(hObject, eventdata, handles)
    % hObject    handle to rho_l (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Hints: get(hObject,'String') returns contents of rho_l as text
    %        str2double(get(hObject,'String')) returns contents of rho_l as a double
    
    
    % --- Executes during object creation, after setting all properties.
    function rho_l_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to rho_l (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called
    
    % Hint: edit controls usually have a white background on Windows.
    %       See ISPC and COMPUTER.
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
    
    
    
    function rho_v_Callback(hObject, eventdata, handles)
    % hObject    handle to rho_v (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Hints: get(hObject,'String') returns contents of rho_v as text
    %        str2double(get(hObject,'String')) returns contents of rho_v as a double
    
    
    % --- Executes during object creation, after setting all properties.
    function rho_v_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to rho_v (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called
    
    % Hint: edit controls usually have a white background on Windows.
    %       See ISPC and COMPUTER.
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
    
    
    
    function edit12_Callback(hObject, eventdata, handles)
    % hObject    handle to edit12 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Hints: get(hObject,'String') returns contents of edit12 as text
    %        str2double(get(hObject,'String')) returns contents of edit12 as a double
    
    
    % --- Executes during object creation, after setting all properties.
    function edit12_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to edit12 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called
    
    % Hint: edit controls usually have a white background on Windows.
    %       See ISPC and COMPUTER.
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
    
    
    
    function edit13_Callback(hObject, eventdata, handles)
    % hObject    handle to edit13 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Hints: get(hObject,'String') returns contents of edit13 as text
    %        str2double(get(hObject,'String')) returns contents of edit13 as a double
    
    
    % --- Executes during object creation, after setting all properties.
    function edit13_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to edit13 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called
    
    % Hint: edit controls usually have a white background on Windows.
    %       See ISPC and COMPUTER.
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
    
    
    
    function h_l_Callback(hObject, eventdata, handles)
    % hObject    handle to h_l (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Hints: get(hObject,'String') returns contents of h_l as text
    %        str2double(get(hObject,'String')) returns contents of h_l as a double
    
    
    % --- Executes during object creation, after setting all properties.
    function h_l_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to h_l (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called
    
    % Hint: edit controls usually have a white background on Windows.
    %       See ISPC and COMPUTER.
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
    
    
    
    function edit15_Callback(hObject, eventdata, handles)
    % hObject    handle to edit15 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Hints: get(hObject,'String') returns contents of edit15 as text
    %        str2double(get(hObject,'String')) returns contents of edit15 as a double
    
    
    % --- Executes during object creation, after setting all properties.
    function edit15_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to edit15 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called
    
    % Hint: edit controls usually have a white background on Windows.
    %       See ISPC and COMPUTER.
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
    
    
    
    function lambda_Callback(hObject, eventdata, handles)
    % hObject    handle to lambda (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Hints: get(hObject,'String') returns contents of lambda as text
    %        str2double(get(hObject,'String')) returns contents of lambda as a double
    
    
    % --- Executes during object creation, after setting all properties.
    function lambda_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to lambda (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called
    
    % Hint: edit controls usually have a white background on Windows.
    %       See ISPC and COMPUTER.
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
    
    
    
    function edit17_Callback(hObject, eventdata, handles)
    % hObject    handle to edit17 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Hints: get(hObject,'String') returns contents of edit17 as text
    %        str2double(get(hObject,'String')) returns contents of edit17 as a double
    
    
    % --- Executes during object creation, after setting all properties.
    function edit17_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to edit17 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called
    
    % Hint: edit controls usually have a white background on Windows.
    %       See ISPC and COMPUTER.
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
    
    
    
    function HTC_Callback(hObject, eventdata, handles)
    % hObject    handle to HTC (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Hints: get(hObject,'String') returns contents of HTC as text
    %        str2double(get(hObject,'String')) returns contents of HTC as a double
    
    
    % --- Executes during object creation, after setting all properties.
    function HTC_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to HTC (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called
    
    % Hint: edit controls usually have a white background on Windows.
    %       See ISPC and COMPUTER.
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
    
    
    % --- Executes on button press in pushbutton_compute.
    function pushbutton_compute_Callback(hObject, eventdata, handles)
    % hObject    handle to pushbutton_compute (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    Tsat=str2num(get(handles.Tsat,'string'));
    
    p_sat=refpropm('P','T',Tsat+273.15,'Q',0,fluid);
    set(handles.Psat,'string',p_sat)
    
    G=str2num(get(handles.G,'string'));
    q=str2num(get(handles.q,'string'))*1000;
    x=str2num(get(handles.x,'string'));
    d_int=str2num(get(handles.d,'string'))*1000;
    
    M = refpropm('M','T',Tsat+273.15,'Q',0,fluid); %kg/mol
    p_crit = refpropm('P','C',0,' ',0,fluid); %kPa
    
    
    lambda_l = refpropm('L','T',Tsat+273.15,'Q',0,fluid);
    sigma = refpropm('I','T',Tsat+273.15,'Q',0,fluid);
    c_l = refpropm('C','T',Tsat+273.15,'Q',0,fluid);
    rho_l = refpropm('D','T',Tsat+273.15,'Q',0,fluid);
    mu_l = refpropm('V','T',Tsat+273.15,'Q',0,fluid);
    mu_v = refpropm('V','T',Tsat+273.15,'Q',1,fluid);
    h_l=refpropm('H','T',Tsat+273.15,'Q',0,fluid); %J/kg
    h_v=refpropm('H','T',Tsat+273.15,'Q',1,fluid); %J/kg
    rho_v = refpropm('D','T',Tsat+273.15,'Q',1,fluid);
    
    set(handles.c_l,'string',c_l)
    set(handles.lambda,'string',lambda_l)
    set(handles.sigma,'string',sigma)
    set(handles.rho_l,'string',rho_l)
    set(handles.rho_v,'string',rho_v)
    set(handles.mu_l,'string',mu_l)
    set(handles.mu_v,'string',mu_v)
    set(handles.h_l,'string',h_l)
    set(handles.h_v,'string',h_v)
    set(handles.Current_condition,'string', 'Psat=',handles.Psat,'bar','G=',handles.G,'kg/m^2s','q=',handles.q,'kW/m^2','d=',handles.d,'mm');  
    
    
    x_plot=0:0.05:1;
    if a==1
        HTC=Gungor_Winterton_1986(x,p_sat,p_crit,G,q,d_int,M,lambda_l,c_l,rho_l,rho_v,mu_l,mu_v,l_evap);
        for i=1:length(x_plot)
        alpha_plot(i)=Gungor_Winterton_1986(x_plot(i),p_sat,p_crit,G,q,d_int,M,lambda_l,c_l,rho_l,rho_v,mu_l,mu_v,l_evap)
        end
    elseif a==2
        HTC=Chien_et_al_2014(x,p_sat,p_crit,G,q,d_int,M,lambda_l,c_l,rho_l,rho_v,mu_l,sigma);
        for i=1:length(x_plot)
        alpha_plot(i)=Chien_et_al_2014(x_plot(i),p_sat,p_crit,G,q,d_int,M,lambda_l,c_l,rho_l,rho_v,mu_l,sigma)
    end
    end
    
        
    plot (handles.axes2,x_plot,alpha_plot)

    e l'errore:
    Undefined function or variable 'fluid'.

    Error in test2>pushbutton_compute_Callback (line 523)
    p_sat=refpropm('P','T',Tsat+273.15,'Q',0,fluid);

    Error in gui_mainfcn (line 95)
    feval(varargin{:});

    Error in test2 (line 42)
    gui_mainfcn(gui_State, varargin{:});

    Error in @(hObject,eventdata)test2('pushbutton_compute_Callback',hObject,eventdata,guidata(hObject))


    Error while evaluating UIControl Callback
  • Re: [GUI] Basi per la programmazione GUI

    Le variabili definite definite all'interno delle callback non solo "visibili" al di fuori di esse: la variabile "fluid" defnita in "popupmenu_fluid_Callback" non è pertanto "visibile" in "pushbutton_compute_Callback" come segnalato dal messaggio di errore.

    Una possibile soluzione per condividere le variabili tra le callback consistge nell'inserirle all'interno della struttura "handles" della GUI.

    Per fare questo puoi usare la funzione "guidata" in questo modo:

    [*] nella callback "popupmenu_fluid_Callback", dopo la definizione del valore di "fluid":
    
    % Get the GUI handles
    my_guidata=guidata(gcf);
    % Add "fluid" to the handles
    my_guidata.fluid=fluid
    % Store the updated GUI handles
    guidata(gcf,my_guidata)
    
    [*] nella callback "pushbutton_compute_Callback"
    
    % Get the GUI handles
    my_guidata=guidata(gcf);
    % Get the "fluid" variable
    fluid=my_guidata.fluid
    
    CAVEAT: al momento non ho accesso a MatLab per cui non posso testare il codice che ho proposto sopra.
  • Re: [GUI] Basi per la programmazione GUI

    La ringrazio per la risposta ma la soluzione da lei proposta non mi risolve il problema: la variabile fluid continua ad essere undefined.
    Undefined function or variable 'fluid'.

    Error in test2>pushbutton_compute_Callback (line 532)
    my_guidata.fluid=fluid;

    Error in gui_mainfcn (line 95)
    feval(varargin{:});

    Error in test2 (line 42)
    gui_mainfcn(gui_State, varargin{:});

    Error in @(hObject,eventdata)test2('pushbutton_compute_Callback',hObject,eventdata,guidata(hObject))


    Error while evaluating UIControl Callback

    Inoltre ho riscontrato che nel popup-menu_correlation solo quando scelgo il primo elemento mi inserisce la stringa desiderata nello Statictext. Cosa c'e' che non va?

    % --- Executes on selection change in correlation.
    function correlation_Callback(hObject, eventdata, handles)
    % hObject handle to correlation (see GCBO)
    % eventdata reserved - to be defined in a future version of MATLAB
    % handles structure with handles and user data (see GUIDATA)

    % Hints: contents = cellstr(get(hObject,'String')) returns correlation contents as cell array
    % contents{get(hObject,'Value')} returns selected item from correlation

    str= cellstr(get(hObject,'String'));
    val=get(hObject,'Value');
    % Determine the selected data set.

    % Set current data to the selected data set.
    switch str{val};
    case 'Gungor & Winterton.1986' % User selects Gungor & Winterton.1986.
    a=1;
    set(handles.Validation,'String','Vertical and horizontal flow in tubes and annuli, Water/R11/R22/R113/R114/Ethylene/Glycol, d=2.95-3.2mm, p=0.08-20.6bar');
    case 'Chien et al.2014 ' % User selects Chien et al.2014.
    a=2;
    set(handles.Validation,'String','Horizontal macro and mini-channels:d=1.5,3.0,6.61,7.49 mm, R410A, G=100-600 kg/m2s, q=10-40 kW/m2, Tsat=5-15 *C, x=0.2-1');

    end
  • Re: [GUI] Basi per la programmazione GUI

    Ho risolto il problema, grazie mille per i consigli utilissimi.
    Ora vorrei inserire in uno static text una stringa che contenga dei valori precedentemente calcolati. Tutto si trova all'interno della stessa Callback.
    Ho scritto una cosa del genere: set(handles.Current_condition,'string', 'Psat=',p_sat,'bar','G=',G,'kg/m^2s','q=',q/1000,'kW/m^2','d=',d_int*1000,'mm');
    ma mi da il seguente errore:
    Error using matlab.ui.control.UIControl/set
    Invalid parameter/value pair arguments.

    Error in test2>pushbutton_compute_Callback (line 627)
    set(handles.Current_condition,'string',
    'Psat=',p_sat,'bar','G=',G,'kg/m^2s','q=',q/1000,'kW/m^2','d=',d_int*1000,'mm');

    Error in gui_mainfcn (line 95)
    feval(varargin{:});

    Error in test2 (line 42)
    gui_mainfcn(gui_State, varargin{:});

    Error in @(hObject,eventdata)test2('pushbutton_compute_Callback',hObject,eventdata,guidata(hObject))


    Error while evaluating UIControl Callback

    Inoltre ho un Pushbottom che mi permette di calcolare delle variabili ed infine di realizzare un plot. Vorrei essere in grado di cancellare il plot realizzato in precedenza ogni volta che premo il pushbottom. Come posso fare?
  • Re: [GUI] Basi per la programmazione GUI

    Nella chiamata alla funzione "set" devi specificare una solo stringa.
    Se vuoi creare una stringa "composta" devi racchiudere le varie parti tra [] inoltre devi convertire i valori numerici delle variabili in stringhe con, per esempio, la funzione "num2str".
    
    set(handles.Current_condition,'string', ['Psat=',num2str(p_sat) ... ])
    
    Per cancellare il plot precedente una soluzione potrebbe essere usare l'istruzione "hold off" oppure usare la funzione "delete" fornendo come inout l'handle del grafico (il valore ritornato, ad esempio, dalla funzione "plot".
  • Re: [GUI] Basi per la programmazione GUI

    Grazie ancora, sempre puntuale e preciso nelle sue risposte.

    Questa e' la porzione di programma relativo a due popup menu.
    function correlation_Callback(hObject, eventdata, handles)
    % hObject handle to correlation (see GCBO)
    % eventdata reserved - to be defined in a future version of MATLAB
    % handles structure with handles and user data (see GUIDATA)

    % Hints: contents = cellstr(get(hObject,'String')) returns correlation contents as cell array
    % contents{get(hObject,'Value')} returns selected item from correlation


    val=get(handles.correlation,'value');
    switch val
    case 1
    a=1;
    set(handles.Validation,'String','Vertical and horizontal flow in tubes and annuli, Water/R11/R22/R113/R114/Ethylene/Glycol, d=2.95-3.2mm, p=0.08-20.6bar');
    case 2
    a=2;
    set(handles.Validation,'String','Horizontal macro and mini-channels:d=1.5,3.0,6.61,7.49 mm, R410A, G=100-600 kg/m2s, q=10-40 kW/m2, Tsat=5-15°C, x=0.2-1');
    case 3
    a=3;
    set(handles.Validation,'String','Horizontal minichannels: d=1.5,3.0 mm , L=2000 mm, R22,R134a,CO2, q=10-40 kW/m2, G=200-600 kg/m2s, Tsat=10°C')
    case 4
    a=4;
    set(handles.Validation,'String',' No condition found')
    case 5
    a=5;
    set(handles.Validation,'String','Horizontal tube d=8 mm, R22,R134a,R125,R410A, Tsat=25-45°C, Pred=0.19-0.53, G=200-600 kg/m2s, q=9-53 kW/m2')
    case 6
    a=6;
    set(handles.Validation,'String','R141b d=1.39-3.69 mm')
    case 7
    a=7;
    set(handles.Validation,'String',' d=3.1mm G=125-750 kg/m2s q=14-380 kW/m2 P=1.3-4.1 bar R113')
    case 8
    a=8;
    set(handles.Validation,'String','Micro channel, Water,R134a, q=159-938 kW/m2, G=127-654 kg/m2s, p=1.44-6.60 bar, x=0.26-0.87')
    case 9
    a=9;
    set(handles.Validation,'String','Water,refrigerants, Ethylene glycol, ethanol, d=2.95-32.0 mm, G=12.4-8179.3 kg/m2s, q=348.9-2.62*10.^6 W/m2, x=0-0.948, Re_lo=568.9-8.75*10.^5, p_red=0.0023-0.895, Pr_lo=0.83-9.1, Fr=2.66*10.^-4-2240')
    case 10
    a=10;
    set(handles.Validation,'String','Vertical tubes: d=0.52,1.1,2.01,2.88,4.26 mm, G=100-500 kg/m2s, q=2.4-175.4 kW/m2, p=6-14 bar')
    case 11
    a=11;
    set(handles.Validation,'String','d=0.21-6.05 mm, G=44-1500 kg/m2s, q=5-109 kW/m2')
    case 12
    a=12;
    set(handles.Validation,'String','d=2.4-2.92 mm, G=44-852 kg/m2s, q=7.5-129 kW/m2, R12/R113/R134a')
    case 13
    a=13;
    set(handles.Validation,'String','Small rectangular channels, d=0.75 mm, 2.7*10^-4<=Bo<=8.9*10^-4, 0.03<=x<=0.55, FC84')
    case 14
    a=14;
    set(handles.Validation,'String','Horizontal tube d=2.98 mm, p=200 kPa, G=50-200 kg/m2s')

    end


    my_guidata=guidata(gcf);
    my_guidata.a=a;
    guidata(gcf,my_guidata);



    % --- Executes during object creation, after setting all properties.
    function correlation_CreateFcn(hObject, eventdata, handles)
    % hObject handle to correlation (see GCBO)
    % eventdata reserved - to be defined in a future version of MATLAB
    % handles empty - handles not created until after all CreateFcns called

    % Hint: popupmenu controls usually have a white background on Windows.
    % See ISPC and COMPUTER.
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
    end


    % --- Executes on selection change in fluid.
    function fluid_Callback(hObject, eventdata, handles)
    % hObject handle to fluid (see GCBO)
    % eventdata reserved - to be defined in a future version of MATLAB
    % handles structure with handles and user data (see GUIDATA)

    % Hints: contents = cellstr(get(hObject,'String')) returns fluid contents as cell array
    % contents{get(hObject,'Value')} returns selected item from fluid
    % Determine the selected data set.

    val=get(handles.fluid,'value');
    switch val
    case 1
    fluid='R134a';
    case 2
    fluid='r410a.mix';
    case 3
    fluid='water';
    case 4
    fluid='ethylene';
    case 5
    fluid='CO2';
    case 6
    fluid='ethanol';

    end

    % Get the GUI handles
    my_guidata=guidata(gcf);
    % Add "fluid" to the handles
    my_guidata.fluid=fluid;
    % Store the updated GUI handles
    guidata(gcf,my_guidata);


    % --- Executes during object creation, after setting all properties.
    function fluid_CreateFcn(hObject, eventdata, handles)
    % hObject handle to fluid (see GCBO)
    % eventdata reserved - to be defined in a future version of MATLAB
    % handles empty - handles not created until after all CreateFcns called

    % Hint: popupmenu controls usually have a white background on Windows.
    % See ISPC and COMPUTER.
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
    end
    Come ha visto i due popup menu sono implementati nello stesso modo, solo che quando vado ad interagire con la GUI cambiando il fluido mi da il seguente errore, cosa che invece non accade con il popup-menu precedente.

    Error using get
    Invalid handle

    Error in test2>fluid_Callback (line 163)
    val=get(handles.fluid,'value');

    Error in gui_mainfcn (line 95)
    feval(varargin{:});

    Error in test2 (line 42)
    gui_mainfcn(gui_State, varargin{:});

    Error in @(hObject,eventdata)test2('fluid_Callback',hObject,eventdata,guidata(hObject))


    Error while evaluating UIControl Callback

    Infine, ho una funzione che mi permette di ottenere un valore che poi viene inserito in un edit text. Mi chiedevo se fosse possibile, interagendo con la GUI, inserire manualmente questo valore e sovrascriverlo al valore calcolato dalla funzione.
  • Re: [GUI] Basi per la programmazione GUI

    Hai provato a "debuggare" il codice?
    Il messaggio di errore sembra chiaro: "fluid" sembra non essere un campo della struttura "handles" oppure il suo contenuto non è l'handle di un uicontrol.
    Prova a mettere un brackpoint nel punto dove viene generato l'errore ed esamina il contenuto di "fluid": dovrebbe esserci qualche differenza rispetto a "correlation" se il secondo "funziona" ed il primo no.
Devi accedere o registrarti per scrivere nel forum
10 risposte