⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 correlation.m

📁 Variable Reduction Testbench通过对变量进行相关性分析来实现减少变量的目的。
💻 M
字号:

function varargout = correlation(varargin)

% -------------------------------------------------------------------------
% this code is part of the 'Reduction Testbench' suite
% developed by A. Manganaro, R. Todeschini, A. Ballabio, D. Mauri
% 2006 - Milano Chemometrics and QSAR Research Group
% -------------------------------------------------------------------------
%
%
% CORRELATION M-file for correlation.fig
%      CORRELATION, by itself, creates a new CORRELATION or raises the existing
%      singleton*.
%
%      H = CORRELATION returns the handle to a new CORRELATION or the handle to
%      the existing singleton*.
%
%      CORRELATION('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in CORRELATION.M with the given input arguments.
%
%      CORRELATION('Property','Value',...) creates a new CORRELATION or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before correlation_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to correlation_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% INIT code 
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @correlation_OpeningFcn, ...
                   'gui_OutputFcn',  @correlation_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin & isstr(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


% --------------------------------------------------------------------


function correlation_OpeningFcn(hObject, eventdata, handles, varargin)

% Executes just before correlation is made visible.
% 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 correlation (see VARARGIN)

% Choose default command line output for correlation
handles.output = hObject;

movegui(handles.correlation_form,'center');

handles.corr_file = ' ';

handles.cur_data = varargin{1};
if isempty(varargin{2})
    handles.header_present = 0;    
else
    handles.cur_headers = varargin{2};
    handles.header_present = 1;
end
handles.is_corrmatrix = varargin{3};

% Update handles structure
guidata(hObject, handles);


% --------------------------------------------------------------------


function varargout = correlation_OutputFcn(hObject, eventdata, handles)

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


% --------------------------------------------------------------------


function file_edit_CreateFcn(hObject, eventdata, handles)

% Executes during object creation, after setting all properties.
% hObject    handle to file_edit (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --------------------------------------------------------------------


function cut_mean_edit_CreateFcn(hObject, eventdata, handles)

% Executes during object creation, after setting all properties.
% hObject    handle to cut_mean_edit (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --------------------------------------------------------------------


function cut_couples_edit_CreateFcn(hObject, eventdata, handles)

% Executes during object creation, after setting all properties.
% hObject    handle to cut_couples_edit (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% GUI objects HANDLERS
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function cut_mean_checkbox_Callback(hObject, eventdata, handles)
% Executes on button press in cut_mean_checkbox.
% hObject    handle to cut_mean_checkbox (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)
if (get(hObject,'Value') == get(hObject,'Max'))
    set(handles.cut_mean_edit,'Enable','on');
else
    set(handles.cut_mean_edit,'Enable','off');
end


% --------------------------------------------------------------------


function cut_couples_checkbox_Callback(hObject, eventdata, handles)
% Executes on button press in cut_couples_checkbox.
% hObject    handle to cut_couples_checkbox (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)if (get(hObject,'Value') == get(hObject,'Max'))
    set(handles.cut_couples_edit,'Enable','on');
else
    set(handles.cut_couples_edit,'Enable','off');
end


% --------------------------------------------------------------------


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

% --------------------------------------------------------------------


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

% --------------------------------------------------------------------


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

% --------------------------------------------------------------------


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


% --------------------------------------------------------------------


function ok_button_Callback(hObject, eventdata, handles)
% Executes on button press in ok_button.
% hObject    handle to ok_button (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)
cut_mean = str2num(get(handles.cut_mean_edit,'String'));
cut_couples = str2num(get(handles.cut_couples_edit,'String'));

if ( (isempty(cut_mean)) | (isempty(cut_couples)) )
    warning_box('Invalid cut value');
    return;
elseif ( ((cut_mean<0)|(cut_mean>1)) | ((cut_couples<0)|(cut_couples>1)) )
    warning_box('Invalid cut value');
    return;
elseif strcmp(handles.corr_file,' ')
    warning_box('Invalid file for txt output');
    return;
end

[out_mean, out_couples] = corr_stats(handles.cur_data,cut_mean,cut_couples,handles.is_corrmatrix);

try
    [file_id mess] = fopen(handles.corr_file, 'wt');
    if (file_id==-1)
        disp('Error opening the log file:');
        disp(mess);
        return;
    end

    if (handles.header_present)

        % Formats the headers
        tmp_h = [{'Name'} handles.cur_headers];
        [tmp_h max_len] = strings_format(tmp_h, 4, 35);
        headers_title = tmp_h{1};
        formatted_headers = tmp_h(2:end);
        
        fprintf(file_id,'Correlation values for each variable (on abs values)\n\n');
        fprintf(file_id,' Var\t%s\tMean \tMax  \tMin\n',headers_title);
        fprintf(file_id,'-------------------------------------------------------------------\n');
        for i=1:length(out_mean)
            fprintf(file_id,'%4.0f\t%s\t%1.3f\t%1.3f\t%1.3f\n',out_mean(i,1),...
                formatted_headers{out_mean(i,1)},out_mean(i,2:4));    
        end

        fprintf(file_id,'\n\nCorrelation values for each couple of variables\n\n');
        fprintf(file_id,'value: Abs  \tReal  \tVar couple\tVar names\n');
        fprintf(file_id,'------------------------------------------------------------------\n');
        for i=1:length(out_couples)
            fprintf(file_id,'       %1.3f\t%+1.3f\t%4.0f %4.0f \t(%s,%s)\n',out_couples(i,1:4),...
                handles.cur_headers{out_couples(i,3)},handles.cur_headers{out_couples(i,4)});    
        end
    else
        fprintf(file_id,'Correlation values for each variable (on abs values)\n\n');
        fprintf(file_id,' Var\tMean \tMax  \tMin\n');
        fprintf(file_id,'----------------------------------------\n');
        for i=1:length(out_mean)
            fprintf(file_id,'%4.0f\t%1.3f\t%1.3f\t%1.3f\n',out_mean(i,1:4));    
        end

        fprintf(file_id,'\n\nCorrelation values for each couple of variables\n\n');
        fprintf(file_id,'value: Abs  \tReal  \tVar couple\n');
        fprintf(file_id,'-----------------------------------------\n');
        for i=1:length(out_couples)
            fprintf(file_id,'       %1.3f\t%+1.3f\t%4.0f %4.0f\n',out_couples(i,1:4));    
        end
    end
    
    fclose(file_id);
catch
    fclose('all');
end
    
close (handles.correlation_form);


% --------------------------------------------------------------------


function file_button_Callback(hObject, eventdata, handles)
% Executes on button press in file_button.
% hObject    handle to file_button (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% Gets the path and filename from the standard file dialog
[cur_filename,cur_pathname] = uiputfile('corr_stats.txt','Save output as text');

if (cur_filename==0)
    cur_corr_file = ' ';
else
    cur_corr_file = strcat(cur_pathname,cur_filename);  
end

handles.corr_file = cur_corr_file;

set(handles.file_edit,'String',handles.corr_file);

% Update handles structure
guidata(hObject, handles);



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Other code
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


function warning_box(cur_msg)
% Calls the msgbox function to show a warning
% cur_msg   message string

msgbox({cur_msg});

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -