请用matlab gui设计一款用于演示单矩孔和圆孔的夫琅禾费衍射用户界面。 该界面能够接受的输入参数如下:菜单栏可以选择单矩孔和圆孔的夫琅禾费衍射,矩形长宽,圆孔的孔径,波长需在界面上手动输入。 该界面的演示效果应为:从菜单栏选取相应的夫琅禾费衍射时,界面刷新到相应的参数填入:矩形孔夫琅禾费衍射需矩形长宽和波长,没有圆孔孔径输入提示;圆孔的夫琅禾费衍射需有孔径大小和波长的填写,无其他参数填写提示点击运行可以出现相应的衍射演示。并给出程序和操作步骤
时间: 2023-12-14 18:35:37 浏览: 76
光学基于matlab GUI单缝夫琅禾费衍射【含Matlab源码 2120期】.zip
5星 · 资源好评率100%
这里提供一个简单的MATLAB GUI代码,可以用于演示单矩孔和圆孔的夫琅禾费衍射。该界面可以选择单矩孔和圆孔的夫琅禾费衍射类型,并要求用户输入相应的参数,例如矩形长宽、圆孔孔径和波长。单击“运行”按钮后,程序将显示相应的衍射图像。
以下是完整的MATLAB GUI代码:
```matlab
function varargout = FraunhoferDiffractionGUI(varargin)
% FRAUNHOFERDIFFRACTIONGUI MATLAB code for FraunhoferDiffractionGUI.fig
% FRAUNHOFERDIFFRACTIONGUI, by itself, creates a new FRAUNHOFERDIFFRACTIONGUI or raises the existing
% singleton*.
%
% H = FRAUNHOFERDIFFRACTIONGUI returns the handle to a new FRAUNHOFERDIFFRACTIONGUI or the handle to
% the existing singleton*.
%
% FRAUNHOFERDIFFRACTIONGUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in FRAUNHOFERDIFFRACTIONGUI.M with the given input arguments.
%
% FRAUNHOFERDIFFRACTIONGUI('Property','Value',...) creates a new FRAUNHOFERDIFFRACTIONGUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before FraunhoferDiffractionGUI_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to FraunhoferDiffractionGUI_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 FraunhoferDiffractionGUI
% Last Modified by GUIDE v2.5 28-Jun-2021 23:11:48
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @FraunhoferDiffractionGUI_OpeningFcn, ...
'gui_OutputFcn', @FraunhoferDiffractionGUI_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 FraunhoferDiffractionGUI is made visible.
function FraunhoferDiffractionGUI_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 FraunhoferDiffractionGUI (see VARARGIN)
% Choose default command line output for FraunhoferDiffractionGUI
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes FraunhoferDiffractionGUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = FraunhoferDiffractionGUI_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 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
% Get the selected value from the popup menu
val = get(hObject, 'Value');
% Enable/disable input boxes based on the selected value
if val == 1 % Rectangular aperture
set(handles.edit1, 'Enable', 'on');
set(handles.text1, 'Enable', 'on');
set(handles.edit2, 'Enable', 'on');
set(handles.text2, 'Enable', 'on');
set(handles.edit3, 'Enable', 'on');
set(handles.text3, 'Enable', 'on');
set(handles.edit4, 'Enable', 'off');
set(handles.text4, 'Enable', 'off');
elseif val == 2 % Circular aperture
set(handles.edit1, 'Enable', 'off');
set(handles.text1, 'Enable', 'off');
set(handles.edit2, 'Enable', 'off');
set(handles.text2, 'Enable', 'off');
set(handles.edit3, 'Enable', 'on');
set(handles.text3, 'Enable', 'on');
set(handles.edit4, 'Enable', 'on');
set(handles.text4, 'Enable', 'on');
end
% --- 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
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
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 edit3_Callback(hObject, eventdata, handles)
% hObject handle to edit3 (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 edit3 as text
% str2double(get(hObject,'String')) returns contents of edit3 as a double
% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit3 (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 button press in pushbutton1.
function pushbutton1_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)
% Get the selected value from the popup menu
val = get(handles.popupmenu1, 'Value');
% Get the input data
if val == 1 % Rectangular aperture
w = str2double(get(handles.edit1, 'String'));
h = str2double(get(handles.edit2, 'String'));
lambda = str2double(get(handles.edit3, 'String'));
% Create the aperture
aperture = rect(w, h);
elseif val == 2 % Circular aperture
D = str2double(get(handles.edit3, 'String'));
lambda = str2double(get(handles.edit4, 'String'));
% Create the aperture
aperture = circ(D/2);
end
% Calculate the diffraction pattern
N = 1024; % Number of sample points in each dimension
L = 10; % Size of the observation plane
dx = L/N; % Sample spacing
x = (-N/2:N/2-1)*dx; % Coordinates
[X,Y] = meshgrid(x); % Meshgrid
k = 2*pi/lambda; % Wavenumber
z = 10000; % Distance from the aperture to the observation plane
A = fftshift(fft2(aperture, N, N)); % Fourier transform of the aperture
H = exp(1i*k*z)*exp(-1i*pi*lambda*z*(X.^2+Y.^2)); % Transfer function
U = ifft2(ifftshift(A.*H)); % Inverse Fourier transform to obtain the diffraction pattern
I = abs(U).^2; % Intensity
% Plot the diffraction pattern
figure;
imshow(I, [], 'XData', x, 'YData', x);
axis on;
colormap(jet);
colorbar;
xlabel('x (m)');
ylabel('y (m)');
% --- Rectangular aperture function
function y = rect(w, h)
x = -w/2:0.01:w/2;
y = zeros(size(x));
y(abs(x) <= w/2) = 1;
y = y.*rectpuls(x/h);
% --- Circular aperture function
function y = circ(r)
x = -r:0.01:r;
y = zeros(size(x));
y(abs(x) <= r) = sqrt(r^2 - x(abs(x) <= r).^2);
y = y.*rectpuls(x/(2*r));
% --- Rectangular pulse function
function y = rectpuls(x)
y = zeros(size(x));
y(abs(x) <= 0.5) = 1;
```
操作步骤:
1. 打开MATLAB,创建一个新的GUI。
2. 在GUI设计器中添加一个“Popup Menu”控件,用于选择夫琅禾费衍射类型(单矩孔或圆孔)。
3. 在GUI设计器中添加四个“Edit Text”控件,用于输入矩形长宽、圆孔孔径和波长。
4. 在GUI设计器中添加一个“Push Button”控件,用于显示夫琅禾费衍射图像。
5. 将上述代码复制到MATLAB编辑器中,并将其保存为“FraunhoferDiffractionGUI.m”文件。
6. 在MATLAB命令窗口中执行“guide FraunhoferDiffractionGUI”命令,打开GUI设计器。
7. 在GUI设计器中选择相应的控件属性,例如位置、大小和字符串。
8. 在GUI设计器中添加回调函数,以响应控件事件(例如,当选择“Popup Menu”控件时,启用/禁用相应的输入框)。
9. 在GUI设计器中保存和构建GUI。
10. 运行GUI,选择相应的夫琅禾费衍射类型并输入参数。
11. 单击“运行”按钮以显示夫琅禾费衍射图像。
阅读全文