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

📄 gui_makecurves.m

📁 利用电磁场的源激发方法来计算光子晶体波导例如光子晶体光纤
💻 M
字号:
function varargout = gui_MakeCurves(varargin)
% GUI_MAKECURVES M-file for gui_MakeCurves.fig
%      GUI_MAKECURVES, by itself, creates a new GUI_MAKECURVES or raises the existing
%      singleton*.
%
%      H = GUI_MAKECURVES returns the handle to a new GUI_MAKECURVES or the handle to
%      the existing singleton*.
%
%      GUI_MAKECURVES('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GUI_MAKECURVES.M with the given input arguments.
%
%      GUI_MAKECURVES('Property','Value',...) creates a new GUI_MAKECURVES or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before gui_MakeCurves_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to gui_MakeCurves_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

% Copyright 2002-2003 The MathWorks, Inc.

% Edit the above text to modify the response to help gui_MakeCurves

% Last Modified by GUIDE v2.5 04-Jun-2006 15:39:55

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @gui_MakeCurves_OpeningFcn, ...
                   'gui_OutputFcn',  @gui_MakeCurves_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 gui_MakeCurves is made visible.
function gui_MakeCurves_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 gui_MakeCurves (see VARARGIN)
handles.pel2micronX = str2double(get(handles.uiXaxis, 'string'));
handles.pel2micronY = str2double(get(handles.uiYaxis, 'string'));
handles.frac = str2double(get(handles.uiFrac, 'string'));
set(handles.uiSlider, 'value', log10(handles.frac));
guidata(hObject, handles);
handles = uiNew_Callback(hObject, eventdata, handles);
handles.output = guidata(hObject);

% Update handles structure
guidata(hObject, handles);
if handles.s ~= -1
    uiwait(handles.figure1);    
end

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


% --- Executes on slider movement.
function uiSlider_Callback(hObject, eventdata, handles)
% hObject    handle to uiSlider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
val = get(hObject, 'Value');
handles.frac = 10^val;
set(handles.uiFrac, 'String', num2str(handles.frac,4));
guidata(hObject, handles);

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

% Hint: slider controls usually have a light gray background, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .9 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

function uiFrac_Callback(hObject, eventdata, handles)
% hObject    handle to uiXaxis (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 uiXaxis as text
%        str2double(get(hObject,'String')) returns contents of uiXaxis as a double
frac = str2double(get(hObject,'String'));
if isnan(frac) | (frac < 1e-4)
    set(hObject, 'string', num2str(handles.frac));
    return
end
handles.frac = frac;
set(handles.uiSlider, 'value', log10(frac))
guidata(hObject, handles);

function uiXaxis_Callback(hObject, eventdata, handles)
% hObject    handle to uiXaxis (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 uiXaxis as text
%        str2double(get(hObject,'String')) returns contents of uiXaxis as a double
pel2micron = str2double(get(hObject,'String'));
if isnan(pel2micron) | (pel2micron <= 0)
    set(hObject, 'string', num2str(handles.pel2micronX));
    return
end
handles.pel2micronX = pel2micron;
[rows, cols] = size(handles.mIm);
handles.x = linspace(-cols/handles.pel2micronX, cols/handles.pel2micronX, cols);
handles.y = linspace(-rows/handles.pel2micronY, rows/handles.pel2micronY, rows);
ShowImage(handles);
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function uiXaxis_CreateFcn(hObject, eventdata, handles)
% hObject    handle to uiXaxis (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 uiYaxis_Callback(hObject, eventdata, handles)
% hObject    handle to uiYAxis (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 uiYAxis as text
%        str2double(get(hObject,'String')) returns contents of uiYAxis as a double
pel2micron = str2double(get(hObject,'String'));
if isnan(pel2micron) | (pel2micron <= 0)
    set(hObject, 'string', num2str(handles.pel2micronY));
    return
end
handles.pel2micronY = pel2micron;
[rows, cols] = size(handles.mIm);
handles.x = linspace(-cols/handles.pel2micronX, cols/handles.pel2micronX, cols);
handles.y = linspace(-rows/handles.pel2micronY, rows/handles.pel2micronY, rows);
ShowImage(handles);
guidata(hObject, handles);



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

if ~isfield(handles, 'oGd')
    h = errordlg('Please press ''Find Curves'' first.', 'Error');
    set(h, 'windowstyle', 'modal');
    waitfor(h);
    return
end
handles.oGd.offsetX = 0;
handles.oGd.offsetY = 0;
guidata(hObject, handles);
handles.output = guidata(hObject);
guidata(hObject, handles);

% Use UIRESUME instead of delete because the OutputFcn needs
% to get the updated handles structure.
uiresume(handles.figure1);

% --- Executes on button press in uiCalc.
function uiCalc_Callback(hObject, eventdata, handles)
% hObject    handle to uiCalc (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
ShowImage(handles);
frac = handles.frac;
x_ax = handles.x;
y_ax = handles.y;
mBW = handles.mIm;
[rows, cols] = size(mBW);
hold on

mBW = double(mBW);
cnt = contourc(x_ax, y_ax, mBW, 1);
v = 1;
h = msgbox('Finding curves in image...', 'SMTgui', 'help', 'modal');
axes(handles.axes1);
while ~isempty(cnt)
        pairs = cnt(2,1);
        xi = cnt(1,2:pairs+1)/1e6;
        yi = cnt(2,2:pairs+1)/1e6;
        cnt = cnt(:, pairs+2:end);
        xc = mean(xi);
        yc = mean(yi);
        if pairs < rows*cols/5000
                continue
        end
        %plot(xc,yc,'b.')
        L = length(xi);
        ind = round(linspace(1, L, round(frac*L)));
        xi = xi(ind);
        yi = yi(ind);
        xi = [xi xi(end)];
        yi = [yi yi(end)];
        N = 500;
        points = [xi - xc ; yi - yc];
        cs = cscvn(points);
        vT = linspace(cs.breaks(1), cs.breaks(end), N);
        mF = ppval(cs, vT);
        xline = mF(1,:) + xc;
        yline = mF(2,:) + yc;
        plot(xline*1e6, yline*1e6, 'b', 'linewidth', 2)
        plot(xi*1e6, yi*1e6, 'r.')
        dt = (vT(2)-vT(1));
        dydt = diff(yline)/dt;
        dxdt = diff(xline)/dt;
        curveArray(v).cs = cs;
        curveArray(v).max_x = max(xline)-xc;
        curveArray(v).max_y = max(yline)-yc;
        curveArray(v).min_x = min(xline)-xc;
        curveArray(v).min_y = min(yline)-yc;
        curveArray(v).xc = xc;
        curveArray(v).yc = yc;
        curveArray(v).iCurve = v;
        curveArray(v).length = sum(sqrt(dydt.^2 + dxdt.^2))*dt;
        drawnow
        v = v + 1;
end
mean_x = mean([curveArray.xc]);
mean_y = mean([curveArray.yc]);
for iCurve = 1:v-1
    curveArray(iCurve).xc = curveArray(iCurve).xc-mean_x;
    curveArray(iCurve).yc = curveArray(iCurve).yc-mean_y;
end
    

delete(h);
hold off
oGd.curveArray = curveArray;
oGd.sdArray = compute_sd_array(curveArray);
oGd.nSd = length(oGd.sdArray);
handles.oGd = oGd;
guidata(hObject, handles);

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

% Use UIRESUME instead of delete because the OutputFcn needs
% to get the updated handles structure.
uiresume(handles.figure1);

% --- Executes on button press in uiNew.
function out = uiNew_Callback(hObject, eventdata, handles)
% hObject    handle to uiNew (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename, pathname, filter] = uigetfile({'*.bmp;*.jpg;*.tif;*.gif;*.pcx;*.pgm;*.ras;*.xwd', 'Image files'},...
    'Open Image');
if filter < 1
    out = handles;
    out.s = -1;
    return
end
out.s = 0;
rgb = imread([pathname filename]);
rgb = double(rgb);
if size(rgb, 3) == 3
    rgb = mean(rgb, 3);
end
rgb = rgb/max(max(rgb));
rgb = rgb(end:-1:1,:);
handles.mIm = rgb;
[rows, cols] = size(rgb);
handles.x = linspace(-cols/handles.pel2micronX, cols/handles.pel2micronX, cols);
handles.y = linspace(-rows/handles.pel2micronY, rows/handles.pel2micronY, rows);
ShowImage(handles);
handles.pathname = pathname;
handles.filename = filename;
guidata(hObject, handles);
out = handles;
out.s = 0;

function ShowImage(handles)
global FontSize
axes(handles.axes1)
imagesc(handles.x, handles.y, handles.mIm)
colormap gray
fixfonts(gca, FontSize);
xlabel('x [\mum]')
ylabel('y [\mum]')
axis image
set(gca, 'ydir', 'normal')
set(gcf, 'doublebuffer', 'on');


% --- Executes during object creation, after setting all properties.
function uiYaxis_CreateFcn(hObject, eventdata, handles)
% hObject    handle to uiYaxis (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 uiFrac_CreateFcn(hObject, eventdata, handles)
% hObject    handle to uiYaxis (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 figure1_CloseRequestFcn(hObject, eventdata, handles)
guidata(hObject, handles);
handles.output = guidata(gcbo);

% Update handles structure
guidata(hObject, handles);
uiresume(handles.figure1);



⌨️ 快捷键说明

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