function varargout = SPEM(varargin)
% SPEM M-file for SPEM.fig
%      SPEM, by itself, creates a new SPEM or raises the existing
%      singleton*.
% Edit the above text to modify the response to help SPEM
% Last Modified by GUIDE v2.5 17-Feb-2009 15:03:32
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @SPEM_OpeningFcn, ...
                   'gui_OutputFcn',  @SPEM_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 SPEM is made visible.
function SPEM_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
if strcmp(get(hObject,'Visible'),'off')
    plot(rand(5));
end
% --- Outputs from this function are returned to the command line.
function varargout = SPEM_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
% --------------------------------------------------------------------
function FileMenu_Callback(hObject, eventdata, handles)
% --------------------------------------------------------------------
function OpenMenuItem_Callback(hObject, eventdata, handles)
clc;
[file_name, pathname, filterindex] = uigetfile( ...
       {'*.xls',  'Excel-files (*.xls)';...
        '*.m;*.fig;*.mat;*.mdl', 'All MATLAB Files (*.m, *.fig, *.mat, *.mdl)';
        '*.m',  'M-files (*.m)'; ...
        '*.fig','Figures (*.fig)'; ...
        '*.mat','MAT-files (*.mat)'; ...
        '*.mdl','Models (*.mdl)'; ...
        '*.*',  'All Files (*.*)'}, ...
        'Pick a file');
Excel=actxserver('Excel.Application');
Workbooks = Excel.Workbooks;
Workbook = invoke(Workbooks, 'Open', [cd,strcat('.\',file_name)]);
read_excel=ddeinit('Excel',file_name);
switch file_name
    case 'beam2.xls'
% --------------------------------------------------------------------
        scale_control=1000;
        Excel_freq_cr=actxserver('Excel.Application');
        Workbooks_cr=Excel_freq_cr.Workbooks;
        Workbook_cr=invoke(Workbooks_cr, 'Open', [cd,'.\beam2.xls']);
        read_excel_freq_cr=ddeinit('Excel','beam2.xls');
        modal_cr=[];
        for j=4:scale_control
            posit_freq_1=strcat('r',num2str(j),'c1');
            posit_freq=strcat(posit_freq_1,':',posit_freq_1);
            modal_cr(j-3,1)=ddereq(read_excel_freq_cr,posit_freq)';      
        end
        invoke(Excel_freq_cr, 'Quit');
        delete(Excel_freq_cr);
        col_scale_count=0;
        for i=1:scale_control-3
            if(modal_cr(i,1)~=1)
                col_scale_count=col_scale_count;                
            else
                col_scale_count=col_scale_count+1;
            end
        end
        mod_num=col_scale_count;        
        row_scale_count=0;
        for i=1:scale_control-3
            if(modal_cr(i,1)==0)
                row_scale_count=row_scale_count;                
            else
                row_scale_count=row_scale_count+1;
            end
        end
        cand_num=row_scale_count/mod_num;
% --------------------------------------------------------------------       
        set(handles.edit7,'String',file_name)
        set(handles.edit1,'String',num2str(cand_num))
        set(handles.edit5,'String',num2str(cand_num-11))
        set(handles.edit6,'String',num2str(mod_num))
        set(handles.edit8,'String',num2str(mod_num-2))     
        set(handles.edit7,'String',file_name)
% --------------------------------------------------------------------
        Excel_freq=actxserver('Excel.Application');
        Workbooks = Excel_freq.Workbooks;
        Workbook = invoke(Workbooks, 'Open', [cd,'.\beam2_freq.xls']);
        read_excel_freq=ddeinit('Excel','beam2_freq.xls');
        for j=1:mod_num
            posit_freq_1=strcat('r',num2str(3+j),'c5');
            posit_freq=strcat(posit_freq_1,':',posit_freq_1);
            modal_freq(j,1)=ddereq(read_excel_freq,posit_freq)';
        end
        invoke(Excel_freq, 'Quit');
        delete(Excel_freq);
    case 'GNTVT.xls'
% --------------------------------------------------------------------        
        scale_control=100;
        Excel_freq_cr=actxserver('Excel.Application');
        Workbooks_cr=Excel_freq_cr.Workbooks;
        Workbook_cr=invoke(Workbooks_cr, 'Open', [cd,'.\GNTVT.xls']);
        read_excel_freq_cr=ddeinit('Excel','GNTVT.xls');
        modal_cr=[];
        for j=4:scale_control
            posit_freq_1=strcat('r',num2str(j),'c1');
            posit_freq=strcat(posit_freq_1,':',posit_freq_1);
            modal_cr(j-3,1)=ddereq(read_excel_freq_cr,posit_freq)';
        end
        invoke(Excel_freq_cr, 'Quit');
        delete(Excel_freq_cr);  
        col_scale_count=0;
        for i=1:scale_control-3
            if(modal_cr(i,1)~=1)
                col_scale_count=col_scale_count;                
            else
                col_scale_count=col_scale_count+1;
            end
        end
        mod_num=col_scale_count;
        row_scale_count=0;
        for i=1:scale_control-3
            if(modal_cr(i,1)==0)
                row_scale_count=row_scale_count;                
            else
                row_scale_count=row_scale_count+1;
            end
        end
        cand_num=row_scale_count/mod_num;    
% --------------------------------------------------------------------
        set(handles.edit7,'String',file_name)
        set(handles.edit1,'String',num2str(cand_num))
        set(handles.edit5,'String','5')
        set(handles.edit6,'String',num2str(mod_num))
        set(handles.edit8,'String','5')
% --------------------------------------------------------------------
        Excel_freq=actxserver('Excel.Application');
        Workbooks = Excel_freq.Workbooks;
         Workbook = invoke(Workbooks, 'Open', [cd,'.\GNTVT_freq.xls']);
        read_excel_freq=ddeinit('Excel','GNTVT_freq.xls');
        for j=1:mod_num
            posit_freq_1=strcat('r',num2str(3+j),'c5');
            posit_freq=strcat(posit_freq_1,':',posit_freq_1);
            modal_freq(j,1)=ddereq(read_excel_freq,posit_freq)';
        end
        invoke(Excel_freq, 'Quit');
        delete(Excel_freq);
    case 'wall_76_xz.xls'
 % --------------------------------------------------------------------        
        scale_control=10000;
        Excel_freq_cr=actxserver('Excel.Application');
        Workbooks_cr=Excel_freq_cr.Workbooks;
        Workbook_cr=invoke(Workbooks_cr, 'Open', [cd,'.\wall_76_xz.xls']);
        read_excel_freq_cr=ddeinit('Excel','wall_76_xz.xls');
        modal_cr=[];
        for j=4:scale_control
            posit_freq_1=strcat('r',num2str(j),'c1');
            posit_freq=strcat(posit_freq_1,':',posit_freq_1);
            modal_cr(j-3,1)=ddereq(read_excel_freq_cr,posit_freq)';      
        end
        invoke(Excel_freq_cr, 'Quit');
        delete(Excel_freq_cr);
        col_scale_count=0;
        for i=1:scale_control-3
            if(modal_cr(i,1)~=1)
                col_scale_count=col_scale_count;                
            else
                col_scale_count=col_scale_count+1;
            end
        end
        mod_num=col_scale_count;        
        row_scale_count=0;
        for i=1:scale_control-3
            if(modal_cr(i,1)==0)
                row_scale_count=row_scale_count;                
            else
                row_scale_count=row_scale_count+1;
            end
        end
        cand_num=row_scale_count/mod_num;
% --------------------------------------------------------------------        
        set(handles.edit7,'String',file_name)
        set(handles.edit1,'String',num2str(cand_num))
        set(handles.edit5,'String',num2str(cand_num-30))
        set(handles.edit6,'String',num2str(mod_num))
        set(handles.edit8,'String',num2str(mod_num-2))       
% --------------------------------------------------------------------                
        Excel_freq=actxserver('Excel.Application');
        Workbooks = Excel_freq.Workbooks;
        Workbook = invoke(Workbooks, 'Open', [cd,'.\wall_76_freq_xz.xls']);
        read_excel_freq=ddeinit('Excel','wall_76_freq_xz.xls');
        for j=1:mod_num
            posit_freq_1=strcat('r',num2str(3+j),'c5');
            posit_freq=strcat(posit_freq_1,':',posit_freq_1);
            modal_freq(j,1)=ddereq(read_excel_freq,posit_freq)';
        end
        invoke(Excel_freq, 'Quit');
        delete(Excel_freq);        
    otherwise
        errordlg('Data file does not exist.','Data Import Error')
end
% --------------------------------------------------------------------
cand_num=get(handles.edit1,'String');
cand_num=str2num(cand_num);
mod_num=get(handles.edit6,'String');
mod_num=str2num(mod_num);
% --------------------------------------------------------------------
modal_data_beam2=zeros(cand_num,2*mod_num);
modal_data_gntvt=zeros(cand_num,2*mod_num);
modal_data_wall=zeros(cand_num,2*mod_num);
modal_data_ting_kau=zeros(cand_num,mod_num);
modal_data_vector_u_beam2=zeros(cand_num,3*mod_num);
modal_data_vector_u_gntvt=zeros(cand_num,3*mod_num);
modal_data_vector_u_wall=zeros(cand_num,3*mod_num);
% --------------------------------------------------------------------
% Construct candidate data set
for j=1:2:2*mod_num
    for i=1:cand_num
        posit1=strcat('r',num2str((j-1)/2+4+(i-1)*mod_num),'c6'); 
        posit2=strcat('r',num2str((j-1)/2+4+(i-1)*mod_num),'c8'); 
        posit=strcat(posit1,':',posit2);        
        modal_data_vector_u(i,1:3)=ddereq(read_excel,posit)';
    end
    switch file_name
    case 'beam2.xls'
        modal_data_beam2(:,j)=modal_data_vector_u(:,1);
        modal_data_beam2(:,j+1)=modal_data_vector_u(:,3);             
    case 'GNTVT.xls'
        modal_data_gntvt(:,j)=modal_data_vector_u(:,1);
        modal_data_gntvt(:,j+1)=modal_data_vector_u(:,3);
    case 'wall_76_xz.xls'
        modal_data_wall(:,j)=modal_data_vector_u(:,1);
        modal_data_wall(:,j+1)=modal_data_vector_u(:,3);    
    otherwise
    errordlg('Data file does not exist.','Data Import Error')
    end
end
invoke(Excel, 'Quit');
delete(Excel);
save open_mat;
% --------------------------------------------------------------------
function PrintMenuItem_Callback(hObject, eventdata, handles)
printdlg(handles.figure1)
% --------------------------------------------------------------------
function CloseMenuItem_Callback(hObject, eventdata, handles)
selection = questdlg(['Close ' get(handles.figure1,'Name') '?'],...
                     ['Close ' get(handles.figure1,'Name') '...'],...
                     'Yes','No','Yes');
if strcmp(selection,'No')
    return;
end
delete(handles.figure1)
% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% --------------------------------------------------------------------
function popupmenu1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
     set(hObject,'BackgroundColor','white');
end

function figure1_ResizeFcn(hObject, eventdata, handles)
% --------------------------------------------------------------------
function Untitled_1_Callback(hObject, eventdata, handles)
% --------------------------------------------------------------------
function Untitled_2_Callback(hObject, eventdata, handles)
% --------------------------------------------------------------------
function Untitled_3_Callback(hObject, eventdata, handles)
% --------------------------------------------------------------------
function Untitled_4_Callback(hObject, eventdata, handles)
helpdlg('Acdemic Use Only. Contact: cejoe@polyu.edu.hk','About SPEM')
% --------------------------------------------------------------------
function pushbutton4_Callback(hObject, eventdata, handles)
close;
% --------------------------------------------------------------------
function edit1_Callback(hObject, eventdata, handles)
num_cand= str2double(get(hObject,'String'));
if isnan(num_cand)
    errodlg('You must entry a number value','Bad Input','modal')
end
cand_num=get(handles.edit1,'String');
set(handles.edit1,'String',cand_num);
guidata(hObject,handles);
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
% --------------------------------------------------------------------
function edit2_Callback(hObject, eventdata, handles)
% --------------------------------------------------------------------
function edit2_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
% --------------------------------------------------------------------
function edit3_Callback(hObject, eventdata, handles)
% --------------------------------------------------------------------
function edit3_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
% --------------------------------------------------------------------
function edit4_Callback(hObject, eventdata, handles)
% --------------------------------------------------------------------
function edit4_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
% --------------------------------------------------------------------
function listbox2_Callback(hObject, eventdata, handles)
% --------------------------------------------------------------------
function listbox2_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
% --------------------------------------------------------------------
function edit5_Callback(hObject, eventdata, handles)
num_targ= str2double(get(hObject,'String'));
if isnan(num_targ)
    errodlg('You must entry a number value','Bad Input','modal')
end
guidata(hObject,handles);
cand_num=get(handles.edit5,'String');
set(handles.edit5,'String',cand_num);

% --------------------------------------------------------------------
function edit5_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
% --------------------------------------------------------------------
function edit6_Callback(hObject, eventdata, handles)
num_mod= str2double(get(hObject,'String'));
if isnan(num_mod)
    errodlg('You must entry a number value','Bad Input','modal')
end
guidata(hObject,handles);
% --------------------------------------------------------------------
function edit6_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
% --------------------------------------------------------------------
function edit7_Callback(hObject, eventdata, handles)

file_name=get(hObject,'String');
if isnan(file_name)
    errodlg('You must entry a number value','Bad Input','modal')
end
guidata(hObject,handles);
% --------------------------------------------------------------------
function edit7_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
% --------------------------------------------------------------------
function Untitled_5_Callback(hObject, eventdata, handles)
function Untitled_6_Callback(hObject, eventdata, handles)
function Untitled_7_Callback(hObject, eventdata, handles)
function Untitled_8_Callback(hObject, eventdata, handles)
function Untitled_9_Callback(hObject, eventdata, handles)
% --------------------------------------------------------------------
function edit8_Callback(hObject, eventdata, handles)
num_mod_exp= str2double(get(hObject,'String'));
if isnan(num_mod_exp)
    errodlg('You must entry a number value','Bad Input','modal')
end
guidata(hObject,handles);
function edit8_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
% --------------------------------------------------------------------
function radiobutton5_Callback(hObject, eventdata, handles)
% Hint: get(hObject,'Value') returns toggle state of radiobutton5
guidata(hObject,handles);
function radiobutton3_Callback(hObject, eventdata, handles)
guidata(hObject,handles);
function radiobutton2_Callback(hObject, eventdata, handles)
guidata(hObject,handles);
function radiobutton4_Callback(hObject, eventdata, handles)
guidata(hObject,handles);
% --------------------------------------------------------------------
% --- Executes during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
guidata(hObject,handles);
% --------------------------------------------------------------------
function Untitled_10_Callback(hObject, eventdata, handles)

guidata(hObject,handles);
% --------------------------------------------------------------------
function Untitled_11_Callback(hObject, eventdata, handles)
set(hObject,'Checked','on');
set(handles.Untitled_12,'Checked','off');
set(handles.Untitled_13,'Checked','off');
guidata(hObject,handles);
% --------------------------------------------------------------------
function Untitled_12_Callback(hObject, eventdata, handles)
set(hObject,'Checked','on');
set(handles.Untitled_11,'Checked','off');
set(handles.Untitled_13,'Checked','off');
guidata(hObject,handles);
% --------------------------------------------------------------------
function Untitled_13_Callback(hObject, eventdata, handles)
set(hObject,'Checked','on');
set(handles.Untitled_11,'Checked','off');
set(handles.Untitled_12,'Checked','off');
guidata(hObject,handles);
% --------------------------------------------------------------------
function Untitled_15_Callback(hObject, eventdata, handles)
set(hObject,'Checked','on');
set(handles.Untitled_16,'Checked','off');
set(handles.Untitled_17,'Checked','off');
guidata(hObject,handles);
% --------------------------------------------------------------------
function Untitled_16_Callback(hObject, eventdata, handles)
set(hObject,'Checked','on');
set(handles.Untitled_15,'Checked','off');
set(handles.Untitled_17,'Checked','off');
guidata(hObject,handles);
% --------------------------------------------------------------------
function Untitled_17_Callback(hObject, eventdata, handles)
set(hObject,'Checked','on');
set(handles.Untitled_15,'Checked','off');
set(handles.Untitled_16,'Checked','off');
guidata(hObject,handles);
% --------------------------------------------------------------------
function Untitled_18_Callback(hObject, eventdata, handles)
num_cand=get(handles.edit1,'String');
num_cand=str2num(num_cand);
num_targ=get(handles.edit5,'String');
num_targ=str2num(num_targ);
num_mod=get(handles.edit6,'String');
num_mod=str2num(num_mod);
num_mod_exp=get(handles.edit8,'String');
num_mod_exp=str2num(num_mod_exp);
file_name=get(handles.edit7,'String');
% --------------------------------------------------------------------
[fim_det,fim_trace,fim_cond,placement_result_efi_no...
    ,fai_initial_efi_no,direction_efi_no,del_sensor_tag_efi_no...
    ,fai_left,tmse_efi_no,mode_shape]=...
    efi_osp(num_mod,num_cand,num_targ,file_name,num_mod_exp);
[fim_det_dpr,fim_trace_dpr,fim_cond_dpr,placement_result_dpr...
    ,fai_initial_dpr,direction_dpr,del_sensor_tag_dpr...
    ,fai_left_dpr,tmse_efi_no_dpr]=...
    efi_dpr_osp(num_mod,num_cand,num_targ,file_name,num_mod_exp);
[fim_det_h,fim_trace_h,fim_cond_h,placement_result_h...
    ,fai_initial_h,direction_h,del_sensor_tag_h,fai_left_h,tmse_h]=...
    homogeneous_osp(num_mod,num_cand,num_targ,file_name,num_mod_exp);
placement_result_r=0;
load sv_placement_result_r;
placement_result_r=placement_result_r;
% --------------------------------------------------------------------
figure(2);
subplot(4,1,1)
stem(placement_result_efi_no,'LineWidth',2.5,'Color','b');
set(gca,'Xtick',1:num_cand,'Fontsize',10);
Ylabel('EFI','Fontsize',20);
grid on;
hold on;
subplot(4,1,2)
stem(placement_result_dpr,'LineWidth',2.5,'Color','r');
set(gca,'Xtick',1:num_cand,'Fontsize',10);
Ylabel('EFI-DPR','Fontsize',20);
grid on;
hold on;
subplot(4,1,3)
stem(placement_result_h,'LineWidth',2.5,'Color','k');
set(gca,'Xtick',1:num_cand,'Fontsize',10);
Ylabel('Uniform','Fontsize',20);
grid on;
hold on;
subplot(4,1,4)
stem(placement_result_r,'LineWidth',2.5,'Color','m');
set(gca,'Xtick',1:num_cand,'Fontsize',10);
Ylabel('Random','Fontsize',20);
grid on;
hold on;
guidata(hObject,handles);
% --------------------------------------------------------------------
function Untitled_19_Callback(hObject, eventdata, handles)
num_cand=get(handles.edit1,'String');
num_cand=str2num(num_cand);
num_targ=get(handles.edit5,'String');
num_targ=str2num(num_targ);
num_mod=get(handles.edit6,'String');
num_mod=str2num(num_mod);
num_mod_exp=get(handles.edit8,'String');
num_mod_exp=str2num(num_mod_exp);
file_name=get(handles.edit7,'String');
% --------------------------------------------------------------------
[fim_det,fim_trace,fim_cond,placement_result_efi_no...
    ,fai_initial_efi_no,direction_efi_no,del_sensor_tag_efi_no...
    ,fai_left,tmse_efi_no,mode_shape]=...
    efi_osp(num_mod,num_cand,num_targ,file_name,num_mod_exp);
figure(3);
[sz_ms_x,sz_ms_y]=size(mode_shape);
% --------------------------------------------------------------------
switch file_name
    case 'beam2.xls'
        for i=1:num_mod
            subplot(4,3,i)
            plot(1:sz_ms_x,mode_shape(:,i),'b--s','MarkerEdgeColor','r','MarkerFaceColor','r','MarkerSize',3,'LineWidth', 1.5)
            set(gca,'Xtick',1:3:num_cand)
            Ylabel(strcat('\Phi',num2str(i)),'Fontsize',20);
            grid on;
            hold on;
        end
    case 'GNTVT.xls'
          for i=1:num_mod
            subplot(2,3,i)
            x_ms=[0 27 121 173 230 272 334 386 443];
            y_ms=mode_shape(:,i);
            y_ms_sp=spline(x_ms,y_ms,0:1:443);
            hp_1=plot(y_ms_sp,0:1:443,'Linewidth',2.5);
            hold on;
            hp_2=plot(y_ms,x_ms,'rs','MarkerEdgeColor','r','MarkerFaceColor','r','MarkerSize',4,'LineWidth', 2.0);
            hold on; 
            axis([-2.5,2.5,0,500]);
            if i==1
                set(gca,'Xtick',0:0.5:1,'Ytick',0:100:500,'Fontsize',10)
                legend([hp_1,hp_2],'Interpolating Curve','Measure Point');
            else
                set(gca,'Xtick',-1:1:1,'Ytick',0:100:500,'Fontsize',10)            
            end
            Ylabel(strcat('\Phi',num2str(i)),'Fontsize',20);
            grid on;
            hold on;
        end
    otherwise
        errordlg('Data file does not exist.','Data Import Error')        
end
guidata(hObject,handles);
% --------------------------------------------------------------------
function Untitled_20_Callback(hObject, eventdata, handles)
guidata(hObject,handles);
% --------------------------------------------------------------------
function pushbutton1_Callback(hObject, eventdata, handles)
efi_ena=0;
efi_dpr_ena=0;
homo_ena=0;
rand_ena=0;
% --------------------------------------------------------------------
num_cand=get(handles.edit1,'String');
num_cand=str2num(num_cand);
num_targ=get(handles.edit5,'String');
num_targ=str2num(num_targ);
num_mod=get(handles.edit6,'String');
num_mod=str2num(num_mod);
num_mod_exp=get(handles.edit8,'String');
num_mod_exp=str2num(num_mod_exp);
file_name=get(handles.edit7,'String');
% --------------------------------------------------------------------
civil_ops_15=get(handles.Untitled_15,'Checked');
civil_ops_16=get(handles.Untitled_16,'Checked');
civil_ops_17=get(handles.Untitled_17,'Checked');
if strcmp(civil_ops_15,'on')
    efi_ena=1;
elseif strcmp(civil_ops_16,'on')
    efi_dpr_ena=1;
elseif strcmp(civil_ops_17,'on')
    efi_dpr_ena=1;
    efi_ena=1;
end
% --------------------------------------------------------------------
civil_ops_11=get(handles.Untitled_11,'Checked');
civil_ops_12=get(handles.Untitled_12,'Checked');
civil_ops_13=get(handles.Untitled_13,'Checked');
if strcmp(civil_ops_11,'on')
    homo_ena=1;
elseif strcmp(civil_ops_12,'on')
    rand_ena=1;
elseif strcmp(civil_ops_13,'on')
    homo_ena=1;
    rand_ena=1;
end
% --------------------------------------------------------------------
h_ap=handles.axes1;
cla;
x=num_cand-num_targ;
set(h_ap,'Xcolor','k','Ycolor','k','Xlim',[0 x],'Ylim',[-10 110]); 
set(h_ap,'Xtick',0:1:x,'Ytick',-10:10:110,'Xgrid','on','Ygrid','on');
hold on;
% --------------------------------------------------------------------
if efi_ena==1
    [fim_det,fim_trace,fim_cond,placement_result_efi_no...
    ,fai_initial_efi_no,direction_efi_no,del_sensor_tag_efi_no...
    ,fai_left,tmse_efi_no,mode_shape]=...
    efi_osp(num_mod,num_cand,num_targ,file_name,num_mod_exp);
% --------------------------------------------------------------------
save fim_det_efi.txt fim_det -ascii;
save mode_shape_efi.txt mode_shape -ascii;
save mode_shape_efi.mat mode_shape;
save del_sensor_tag_efi.txt del_sensor_tag_efi_no -ascii;  
% --------------------------------------------------------------------
[sz_del_st_x,sz_del_st_y]=size(del_sensor_tag_efi_no);
del_sensor_tag_efi_no=cat(2,0,del_sensor_tag_efi_no);
hplot1=plot(0:x,fim_det(1,:).*100./(fim_det(1,1)),'b--o','LineWidth',2.5...
    ,'MarkerSize',10);
hold on;
end
% --------------------------------------------------------------------
if efi_dpr_ena==1
    [fim_det_dpr,fim_trace_dpr,fim_cond_dpr,placement_result_dpr...
    ,fai_initial_dpr,direction_dpr,del_sensor_tag_dpr,...
    fai_left_dpr,tmse_efi_no_dpr]=...
    efi_dpr_osp(num_mod,num_cand,num_targ,file_name,num_mod_exp);
[sz_del_st_x,sz_del_st_y]=size(del_sensor_tag_dpr);
del_sensor_tag_dpr=cat(2,0,del_sensor_tag_dpr);
hplot2=plot(0:x,fim_det_dpr(1,:).*100./(fim_det_dpr(1,1)),'r--s','LineWidth',2.5...
    ,'MarkerSize',10);
hold on;
end
% --------------------------------------------------------------------
if homo_ena==1
    [fim_det_h,fim_trace_h,fim_cond_h,placement_result_h,fai_initial_h,direction_h,del_sensor_tag_h,...
    fai_left_h,tmse_h]=...
    homogeneous_osp(num_mod,num_cand,num_targ,file_name,num_mod_exp);
[sz_del_st_x,sz_del_st_y]=size(del_sensor_tag_h);
del_sensor_tag_h=cat(2,0,del_sensor_tag_h);
hplot3=plot(0:x,fim_det_h(1,:).*100./(fim_det_h(1,1)),'k--x','LineWidth',2.5...
    ,'MarkerSize',10);
hold on;
end
% --------------------------------------------------------------------
if rand_ena==1
    [fim_det_r,fim_trace_r,fim_cond_r,placement_result_r...
    ,fai_initial_r,direction_r,del_sensor_tag_r...
    ,fai_left_r,tmse_r]=...
    random_osp(num_mod,num_cand,num_targ,file_name,num_mod_exp);
[sz_del_st_x,sz_del_st_y]=size(del_sensor_tag_r);
del_sensor_tag_r=cat(2,0,del_sensor_tag_r);
hplot4=plot(0:x,fim_det_r(1,:).*100./(fim_det_r(1,1)),'m--+','LineWidth',2.5...
    ,'MarkerSize',10);
hold on;
save sv_placement_result_r placement_result_r;
end
%--------------------------------------------------------------------
if efi_ena==1
    haxis=get(hplot1, 'Parent');
else 
    haxis=get(hplot2, 'Parent');
end
ylabel('FIM Quality(%)','FontSize',20);
hold on;
radio_value_3=get(handles.radiobutton3,'Value');
%--------------------------------------------------------------------
if radio_value_3==1
    if efi_ena==1
        set(h_ap,'Xticklabel',del_sensor_tag_efi_no);
        xlabel('ID of sensors deleted by EFI','FontSize',20);
        set(haxis, 'LineWidth', 1.0, 'FontSize',15,'Xcolor','b');         
    else
        set(h_ap,'Xticklabel',del_sensor_tag_dpr);
        xlabel('ID of sensors deleted by EFI-DPR','FontSize',20);
        set(haxis, 'LineWidth', 1.0, 'FontSize',15,'Xcolor','r'); 
    end
else
    if efi_dpr_ena==1
        set(h_ap,'Xticklabel',del_sensor_tag_dpr);
        xlabel('ID of sensors deleted by EFI-DPR','FontSize',20);
        set(haxis, 'LineWidth', 1.0, 'FontSize',15,'Xcolor','r'); 
    else
        set(h_ap,'Xticklabel',del_sensor_tag_efi_no);
        xlabel('ID of sensors deleted by EFI','FontSize',20);
        set(haxis, 'LineWidth', 1.0, 'FontSize',15,'Xcolor','b');                  
    end
end
%--------------------------------------------------------------------
h_ap=handles.axes1;
h_at=axes('Position',get(h_ap,'Position'));
hold on;
set(h_at,'Xaxislocation','top'); 
set(h_at,'Yaxislocation','right','YMinorGrid','off','YMinorTick','off'); 
set(h_at,'Xcolor','k','Ycolor','k','Xlim',[0 x],'Ylim',[-10 110]);
hold on;
xpm=get(h_at,'Xlim');
txtick=xpm(1):1:xpm(2); 
ypm=get(h_at,'Ylim');
tytick=ypm(1):10:ypm(2); 
set(h_at,'Xtick',txtick,'Ytick',tytick,'Xgrid','on','Ygrid','on');
radio_value_4=get(handles.radiobutton4,'Value');
%--------------------------------------------------------------------
if radio_value_4==1
    if homo_ena==1
        set(h_at,'Xticklabel',del_sensor_tag_h,'Yticklabel',[]);
        xlabel('ID of sensors deleted by uniform removal','FontSize',20);
        set(h_at, 'LineWidth', 1.0, 'FontSize',15,'Xcolor','k');         
    else
        set(h_at,'Xticklabel',del_sensor_tag_r,'Yticklabel',[]);
        xlabel('ID of sensors deleted by random removal','FontSize',20);
        set(h_at, 'LineWidth', 1.0, 'FontSize',15,'Xcolor','m'); 
    end
else
    if rand_ena==1
        set(h_at,'Xticklabel',del_sensor_tag_r,'Yticklabel',[]);
        xlabel('ID of sensors deleted by random removal','FontSize',20);
        set(h_at, 'LineWidth', 1.0, 'FontSize',15,'Xcolor','m');         
    else
        set(h_at,'Xticklabel',del_sensor_tag_h,'Yticklabel',[]);
        xlabel('ID of sensors deleted by uniform removal','FontSize',20);
        set(h_at, 'LineWidth', 1.0, 'FontSize',15,'Xcolor','k');  
    end
end
%--------------------------------------------------------------------
hold on;
set(h_at,'Color','none'); 
set(h_ap,'Color','white'); 
if (efi_ena==1)&&(efi_dpr_ena==1)&&(homo_ena==1)&&(rand_ena==1)
    legend([hplot1,hplot2,hplot3,hplot4],'EFI ','EFI-DPR','Uniform','Random');
elseif (efi_ena==1)&&(efi_dpr_ena==0)&&(homo_ena==1)&&(rand_ena==1)
    legend([hplot1,hplot3,hplot4],'EFI ','Uniform','Random');
end
grid on;
guidata(hObject,handles);