Matlab Gui:如何在函数之间共享数据

时间:2014-01-19 11:17:03

标签: matlab user-interface

我是这个论坛和matlab GUI的新手。我有这个问题。我想创建一个GUI,允许您从xlsx文件加载数据,然后我想用这些数据计算GARCH模型。我遇到的问题是我不知道如何将这些数据传递给函数GARCH。

这些是我写的功能。希望你能帮帮我。

function open(handles)
[FileName, PathName] = uigetfile('*.xlsx', 'Seleziona File dei Rendimenti');
A = xlsread(FileName, -1);
inVdati = A(:);
plot(inVdati, 'r');
legend('returns');
set(handles.Variance, 'Enable', 'on');
guidata(handles.figure1, handles) %saving values

and

function garch(handles)
handles = guidata(handles.figure1);
Sigmas = [];
spec = garchset('P', 1, 'Q', 1);
[Coeff, Errors, LLF, Innovations, Sigmas] = garchfit(spec, handles);

你们有人能告诉我哪里错了吗?

谢谢你:)

如果您需要,这是完整的代码:

function varargout = Var_final(varargin)
% VAR_FINAL MATLAB code for Var_final.fig
%      VAR_FINAL, by itself, creates a new VAR_FINAL or raises the existing
%      singleton*.
%
%      H = VAR_FINAL returns the handle to a new VAR_FINAL or the handle to
%      the existing singleton*.
%
%      VAR_FINAL('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in VAR_FINAL.M with the given input arguments.
%
%      VAR_FINAL('Property','Value',...) creates a new VAR_FINAL or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Var_final_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Var_final_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 Var_final

% Last Modified by GUIDE v2.5 18-Jan-2014 22:34:22

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Var_final_OpeningFcn, ...
                   'gui_OutputFcn',  @Var_final_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 Var_final is made visible.
function Var_final_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 Var_final (see VARARGIN)
% Choose default command line output for Var_final
handles.figure1 = hObject;
 % Update handles structure
guidata(hObject, handles);
handles.inVdati = [];
initialize_gui(handles);

% UIWAIT makes Var_final wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = Var_final_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.figure1;



function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (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 edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (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 selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject    handle to listbox1 (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 listbox1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox1


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

% Hint: listbox 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 listbox2.
function listbox2_Callback(hObject, eventdata, handles)
% hObject    handle to listbox2 (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 listbox2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox2


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

% Hint: listbox 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 edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (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 edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double


% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (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 edit4_Callback(hObject, eventdata, handles)
% hObject    handle to edit4 (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 edit4 as text
%        str2double(get(hObject,'String')) returns contents of edit4 as a double


% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit4 (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 selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (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 popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1


% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (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 popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (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 popupmenu2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu2
handles.dati.strutt = get(hObject, 'Value');

% --- Executes during object creation, after setting all properties.
function popupmenu2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (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 button press in radiobutton1.
function radiobutton1_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton1


% --- Executes on button press in radiobutton2.
function radiobutton2_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton2


% --- Executes on button press in radiobutton3.
function radiobutton3_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton3



function edit7_Callback(hObject, eventdata, handles)
% hObject    handle to edit7 (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 edit7 as text
%        str2double(get(hObject,'String')) returns contents of edit7 as a double


% --- Executes during object creation, after setting all properties.
function edit7_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit7 (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_calcola.
function pushbutton_calcola_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_calcola (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
calcola(handles)

% --- Executes on button press in pushbutton_reset.
function pushbutton_reset_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_reset (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.edit4, 'value', 0)

% --------------------------------------------------------------------
function M_File_Callback(hObject, eventdata, handles)
% hObject    handle to M_File (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function AA_File_Callback(hObject, eventdata, handles)
% hObject    handle to AA_File (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function A_File_Callback(hObject, eventdata, handles)
% hObject    handle to A_File (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
open(handles)

% --------------------------------------------------------------------
function TB_Reset_ClickedCallback(hObject, eventdata, handles)
% hObject    handle to TB_Reset (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
gcbo(handles.figure1,handles)

% --------------------------------------------------------------------
function TB_Salva_ClickedCallback(hObject, eventdata, handles)
% hObject    handle to TB_Salva (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
save_output(handles)


% --------------------------------------------------------------------
function TB_Apri_ClickedCallback(hObject, eventdata, handles)
% hObject    handle to TB_Apri (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
open(handles)





function initialize_gui(handles)
%questa funzione serve a definire i valori iniziali della GUI e quando
%viene premuto il pulsante reset
%struttura dati
handles.inputs.w=0;
handles.inputs.V_0=0;
handles.inputs.dist=0;
handles.inputs.alpha=0;
handles.dati.strutt=1;
handles.dati.strutt1=1;
handles.inputs.returns=0;
%struttura vettori delle funzioni risolutrici
handles.vettori.returns=[];
handles.vettori.sigmas=[];
handles.vettori.coeff=[];
handles.vettori.errors=[];
handles.vettori.LLF=[];
handles.vettori.Innovations=[];
handles.vettori.VAR=[];
%salvo le strutture create
guidata(handles.figure1, handles);
%passiamo i dati edit_text
set(handles.popupmenu1, 'Value', 1);
set(handles.popupmenu2, 'Value', 1);
set(handles.edit4, 'String', handles.inputs.V_0);
%nascondo assi all'apertura della GUI
set(handles.axes1, 'Visible', 'off');
set(handles.axes2, 'Visible', 'off');
set(handles.Variance, 'Enable', 'off');
cla;

%For Saving Outputs
function save_output(handles)
[Filename, Pathname] = uiputfile('.bmp', 'Save As');
Name = fullfile(Filename, Pathname);
imwrite(img, Filename, 'bmp');
[Filename, Pathname] = uiputfile('*.bmp', 'Save As');
if Pathname==0, return;
end

function garch(handles)
handles = guidata(handles.figure1, inVdati);
Sigmas = [];
spec = garchset('P', 1, 'Q', 1);
[Coeff, Errors, LLF, Innovations, Sigmas] = garchfit(spec, handles);
plot(Sigmas);


% --------------------------------------------------------------------
function E_File_Callback(hObject, eventdata, handles)
% hObject    handle to E_File (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function Untitled_1_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function R_File_Callback(hObject, eventdata, handles)
% hObject    handle to R_File (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
initialize_gui(handles)

%caricare i rendimenti
function open(handles)
[FileName, PathName] = uigetfile('*.xlsx', 'Seleziona File dei Rendimenti');
A = xlsread(FileName, -1);
inVdati = A(:);
plot(inVdati, 'r');
legend('returns');
set(handles.Variance, 'Enable', 'on');
guidata(handles.figure1, inVdati) %saving values

% --- Executes on button press in Variance.
function Variance_Callback(hObject, eventdata, handles)
% hObject    handle to Variance (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
garch(handles)

function tabella_dati(handles)
inVdati = guidata(gcbo);
%figura principale
form1=figure('Position', [400,300,600,400], 'Name', 'Tabella Dati', ...
    'NumberTitle', 'off', 'Color', [0.941 0.941 0.941], ...
    'Resize', 'off', 'WindowStyle', 'normal');
%statit text
testo = uicontrol(form1, 'Style', 'text', 'Position', [250, 370, 100, 25], ...
    'String', 'Dati Numerici');
%Table
cnames = {'Returns', 'Sigmas', 'Correlation', 'VaR'};
table = uitable('Data', 'inVdati', 'ColumnName', cnames,...
    'Parent', form1, 'Position', [10 50 580 320]);
%Pulsanti
Esporta_Excel = uicontrol(form1, 'Style', 'pushbutton',...
    'Position', [20, 10, 70, 25], 'String', 'Esporta in Excel', ...
    'Callback', @esporta_excel_plot);
Esporta_txt = uicontrol(form1, 'Style', 'pushbutton', ...
    'Position', [110, 10, 70, 25], ...
    'String', 'Esporta txt', 'Callback', @esporta_txt_plot);

chiudi = uicontrol(form1, 'Style', 'pushbutton', 'Position', [200, 10, 70, 25], ...
    'String', 'Chiudi', 'Callback', @chiudi_plot);




% --- Executes on button press in parametric.
function parametric_Callback(hObject, eventdata, handles)
% hObject    handle to parametric (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of parametric


% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
axes(handles.axes1);
plot(guidata(gcbo));
axes(handles.axes2);
plot(Sigmas);


% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
tabella_dati(handles)

1 个答案:

答案 0 :(得分:0)

您可以使用句柄结构保存您希望在不同函数之间使用的变量。例如,如果我想将数组A传递给另一个函数:

% --- Executes on button press in push_button1.
function push_button1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
A = 0:0.1:2;
handles.array = A;
guidata(hObject,handles);

现在在另一个功能下,我可以访问A到:

% --- Executes on button press in push_button2.
function push_button2_Callback(hObject, eventdata, handles)
% hObject    handle to push_button2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
B = handles.array;

或者甚至通过编写

将其传递给另一个函数
output = some_func(handles.array)