vectgui.m

来自「matlab源代码,适用于开发研究,带来很好的学习效果.」· M 代码 · 共 343 行

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

% Last Modified by GUIDE v2.5 17-Jul-2006 12:20:16

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @vectgui_OpeningFcn, ...
                   'gui_OutputFcn',  @vectgui_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 vectgui is made visible.
function vectgui_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 vectgui (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


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




function Y_Comp_Callback(hObject, eventdata, handles)
% hObject    handle to Y_Comp (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 Y_Comp as text
%        str2double(get(hObject,'String')) returns contents of Y_Comp as a double
set(handles.Draw,'Enable','On');         % Enable Draw_pushbutton after function is available
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function Y_Comp_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Y_Comp (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 Z_Comp_Callback(hObject, eventdata, handles)
% hObject    handle to Z_Comp (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 Z_Comp as text
%        str2double(get(hObject,'String')) returns contents of Z_Comp as a double
set(handles.Draw,'Enable','On');         % Enable Draw_pushbutton after function is available
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function Z_Comp_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Z_Comp (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 X_range_Callback(hObject, eventdata, handles)
% hObject    handle to X_range (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 X_range as text
%        str2double(get(hObject,'String')) returns contents of X_range as a double
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function X_range_CreateFcn(hObject, eventdata, handles)
% hObject    handle to X_range (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 Y_range_Callback(hObject, eventdata, handles)
% hObject    handle to Y_range (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 Y_range as text
%        str2double(get(hObject,'String')) returns contents of Y_range as a double
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function Y_range_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Y_range (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 Z_range_Callback(hObject, eventdata, handles)
% hObject    handle to Z_range (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 Z_range as text
%        str2double(get(hObject,'String')) returns contents of Z_range as a double
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function Z_range_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Z_range (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


% --- Executes on button press in Draw.
function Draw_Callback(hObject, eventdata, handles)
% hObject    handle to Draw (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
cla;        % Clean the figure

x_range = str2num(get(handles.X_range,'String'));
y_range = str2num(get(handles.Y_range,'String'));
z_range = str2num(get(handles.Z_range,'String'));

x_comp = get(handles.X_Comp,'String');
y_comp = get(handles.Y_Comp,'String');
z_comp = get(handles.Z_Comp,'String');

xmin = x_range(1);
xmax = x_range(2);
ymin = y_range(1);
ymax = y_range(2);
zmin = z_range(1);
zmax = z_range(2);

dx = (xmax-xmin)/4;
dy = (ymax-ymin)/4;
dz = (zmax-zmin)/4;

if dx ~= 0
    xm = xmin:dx:xmax;
else
    xm = zeros(1,5);
end
      
if dy ~= 0
    ym = ymin:dy:ymax;
else
    ym = zeros(1,5);
end
       
if dz ~= 0
    zm = zmin:dz:zmax;
else
    zm = zeros(1,5);
end

[meshx,meshy,meshz]=meshgrid(xm,ym,zm);             % construct data points

if x_comp ~= '0'
    x_fun = inline(x_comp,'x','y','z');             % construct inline object for x component
    xc = x_fun(meshx,meshy,meshz);
else
    xc = zeros(5,5,5);
end

if y_comp ~= '0'
    y_fun = inline(y_comp,'x','y','z');             % construct inline object for y component
    yc = y_fun(meshx,meshy,meshz);
else
    yc = zeros(5,5,5);
end

if z_comp ~= '0'
    z_fun = inline(z_comp,'x','y','z');             % construct inline object for z component
    zc = z_fun(meshx,meshy,meshz);
else
    zc = zeros(5,5,5);
end

axes(handles.field_axes)                            % choose field_axes to plot the field
handles.field_axes_handle = gca;                    % return handle of field_axes to workspace
quiver3(meshx,meshy,meshz,xc,yc,zc)                 % plot the vector field
axis vis3d
xlabel('x')
ylabel('y')
zlabel('z')
grid on

[curlx,curly,curlz]=curl(meshx,meshy,meshz,xc,yc,zc);       % calculate curl
axes(handles.curl_axes)                             % choose curl_axes to plot the curl
handles.curl_axes_handle = gca;                     % return handle of curl_axes to workspace
quiver3(meshx,meshy,meshz,curlx,curly,curlz)        % plot curl of the field
axis vis3d
xlabel('x')
ylabel('y')
zlabel('z')
grid on

div = divergence(meshx,meshy,meshz,xc,yc,zc);      % calculate divergence

handles.position = [meshx(:),meshy(:),meshz(:)];   % return coordinates to workspace
handles.vector = [xc(:),yc(:),zc(:)];              % return vector components to workspace
handles.divergence = div(:);                       % return divergence to workspace
handles.curl = [curlx(:),curly(:),curlz(:)];       % return curl to workspace

set(handles.Output,'Enable','On');      % enable output_pushbutton after draw

guidata(hObject, handles);

% --- Executes on button press in Output.
function Output_Callback(hObject, eventdata, handles)
% hObject    handle to Output (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.Close,'Enable','On');      % enable close_pushbutton after output
uiresume    % resume GUI

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

function X_Comp_Callback(hObject, eventdata, handles)
% hObject    handle to X_Comp (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 X_Comp as text
%        str2double(get(hObject,'String')) returns contents of X_Comp as a double
set(handles.Draw,'Enable','On');         % Enable Draw_pushbutton after function is available
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function X_Comp_CreateFcn(hObject, eventdata, handles)
% hObject    handle to X_Comp (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


⌨️ 快捷键说明

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