使用matlab GUI编写坐标正反算程序

在学习MATLAB测量程序设计这门课中,编写了一个用于测量学中坐标正反算的GUI界面,记录如下

一、设计界面

首先,启动GUIDE:在命令行中键入GUIDE命令启动GUIDE,弹出下面窗口:

选择BlankGUI,确定,会创建一个设计GUI 界面的窗口:

在该窗口中可以通过控件的拖拽对界面进行设计和布局,本文主要记录控件功能的编写,对GUI界面的设计具体使用方法在此不再阐述。

设计好的界面命名为poeitive_negative_Coordinates.fig,如下图所示:

设计好的界面会自动生成一个.m文件用来编写程序实现相应控件的功能,本文在对应的poeitive_negative_Coordinates.m文件中对控件功能进行实现

二、功能实现

图像清零按钮:

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)
axes(handles.axes1); %指定需要清空的坐标轴
cla reset;

数据清零按钮:

function pushbutton7_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(findobj('style','edit'),'string','')%清空edit文本数据

界面退出按钮:

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

坐标正算组合功能:

function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% 坐标正算:
ya = str2num(get(handles.edit2,'string')); % 读取已测点竖坐标
xa = str2num(get(handles.edit1,'string')); % 读取已测点横坐标
D = str2num(get(handles.edit6,'string'));  % 读取两点间距离
du = str2num(get(handles.edit3,'string')); % 读取方位角度数
fen = str2num(get(handles.edit4,'string')); % 读取方位角分数
miao = str2num(get(handles.edit5,'string')); % 读取方位角秒数
a = du + fen/60 + miao/3600; % 计算方位角
yb = ya + D * cos(pi * a/180);% 计算未知点竖坐标
xb = xa + D * sin(pi * a/180);% 计算未知点横坐标
set(handles.edit8,'string',num2str(yb)); %在界面上显示待测点竖坐标
set(handles.edit7,'string', num2str(xb)); % 在界面上显示待测点横坐标

% 绘制图像
% axes(handles,axes1); % 打开坐标轴
plot(xa,ya,'ro',xb,yb,'ro','MarkerSize',12); % 画已测点和待测点
hold on; % 保持图像
plot ([xa,xb],[ya,yb], 'k','LineWidth',2);
grid on;
text(xa,ya,'A 点','Fontsize',16);text(xb,yb,'B 点','Fontsize',16); % 画直线段与标注点
plot([xa,xa], [ya,yb], 'b-.');plot([xa,xb],[yb,yb],'b-.');   % 画两虚线段
x0 = max(xa,xb); y0 = max(ya,yb); x2 = min(xa,xb); y2 = min(ya,yb);
axis([x2-0.1*x2,x0+0.1*x0,y2-0.1*y2,y0+0.1*y0]); axis('equal');%  确定坐标轴范围
xlabel('Y 轴');ylabel('X 轴'); % 标注横竖坐标轴
hold off; % 关闭图像

坐标反算组合功能:

实现此功能时注意方位角和象限角的转换
注意弧度制和角度值
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% 计算距离 D2
ya2 = str2num(get(handles.edit10,'string')); 
xa2 = str2num(get(handles.edit9,'string')); 

yb2 = str2num(get(handles.edit12,'string')); 
xb2 = str2num(get(handles.edit11,'string'));

xab = xb2 - xa2;yab = yb2-ya2;
D2 = num2str(sqrt(yab^2+xab^2));
set(handles.edit16,'string',num2str(D2)); 

% 绘制图像
% axes(handles,axes1); % 打开坐标轴
plot(xa2,ya2,'ro',xb2,yb2,'ro','MarkerSize',12); % 画已测点和待测点
hold on; % 保持图像
plot ([xa2,xb2],[ya2,yb2], 'k','LineWidth',2);
grid on;
text(xa2,ya2,'A 点','Fontsize',16);text(xb2,yb2,'B 点','Fontsize',16); % 画直线段与标注点
plot([xa2,xa2], [ya2,yb2], 'b-.');plot([xa2,xb2],[yb2,yb2],'b-.');   % 画两虚线段
x02 = max(xa2,xb2); y02 = max(ya2,yb2); x22 = min(xa2,xb2); y22 = min(ya2,yb2);
axis([x22-0.1*x22,x02+0.1*x02,y22-0.1*y22,y02+0.1*y02]); axis('equal');%  确定坐标轴范围
xlabel('Y 轴');ylabel('X 轴'); % 标注横竖坐标轴
hold off; % 关闭图像

% 计算方位角
fwj = atan(yab/xab);
fwj = rad2deg(fwj);
if xab>0&yab>0
    fwj = fwj;
elseif xab>0&yab<0
    fwj = 360 + fwj;
elseif xab<0&yab<0
    fwj = 180 + fwj;
elseif xab<0&yab>0
    fwj = 180 + fwj;
elseif xab == 0&yab>0
    fwj = 0;
elseif xab == 0&yab<0
    fwj = 180;
elseif yab == 0&xab<0
    fwj = 270;
elseif yab == 0&xab>0
    fwj = 90;
end

du = fix(fwj); fen = fix((fwj - du)*60); miao = 60*((fwj - du)*60 - fen); % 计算方位角的度分秒数
du = num2str(du);fen = num2str(fen);miao = num2str(miao);
set(handles.edit13,'string',du);set(handles.edit14,'string',fen);set(handles.edit15,'string',miao); %  显示方位角的度分秒数

三、程序运行效果

界面总览:

坐标正算:

坐标反算:

四、注意事项

  1. 如果坐标反算计算出的角度错误,可能是由于函数 atan() 计算出的角度为弧度制,而界面上得出的是角度值,要将结果转化成角度值在进行方位角的转化以及数据显示

fwj = rad2deg(fwj);
  1. 设计界面的时候可以给部分主要控件更改具有实际意义名称,以便在添加功能和后续更改、阅读时更加清晰直观

五、完整代码

MATLAB GUI 生成的.m文件会有大量注释,因此所有代码较长,在此主要是提供一个大体结构的参考
function varargout = positive_negative_Coordinates(varargin)
% POSITIVE_NEGATIVE_COORDINATES MATLAB code for positive_negative_Coordinates.fig
%      POSITIVE_NEGATIVE_COORDINATES, by itself, creates a new POSITIVE_NEGATIVE_COORDINATES or raises the existing
%      singleton*.
%
%      H = POSITIVE_NEGATIVE_COORDINATES returns the handle to a new POSITIVE_NEGATIVE_COORDINATES or the handle to
%      the existing singleton*.
%
%      POSITIVE_NEGATIVE_COORDINATES('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in POSITIVE_NEGATIVE_COORDINATES.M with the given input arguments.
%
%      POSITIVE_NEGATIVE_COORDINATES('Property','Value',...) creates a new POSITIVE_NEGATIVE_COORDINATES or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before positive_negative_Coordinates_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to positive_negative_Coordinates_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 positive_negative_Coordinates

% Last Modified by GUIDE v2.5 07-Mar-2023 22:39:09

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = positive_negative_Coordinates_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 button press in togglebutton1.
function togglebutton1_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of togglebutton1


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

% --- 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)
axes(handles.axes1); %指定需要清空的坐标轴
cla reset;

% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% 坐标正算:
ya = str2num(get(handles.edit2,'string')); % 读取已测点竖坐标
xa = str2num(get(handles.edit1,'string')); % 读取已测点横坐标
D = str2num(get(handles.edit6,'string'));  % 读取两点间距离
du = str2num(get(handles.edit3,'string')); % 读取方位角度数
fen = str2num(get(handles.edit4,'string')); % 读取方位角分数
miao = str2num(get(handles.edit5,'string')); % 读取方位角秒数
a = du + fen/60 + miao/3600; % 计算方位角
yb = ya + D * cos(pi * a/180);% 计算未知点竖坐标
xb = xa + D * sin(pi * a/180);% 计算未知点横坐标
set(handles.edit8,'string',num2str(yb)); %在界面上显示待测点竖坐标
set(handles.edit7,'string', num2str(xb)); % 在界面上显示待测点横坐标

% 绘制图像
% axes(handles,axes1); % 打开坐标轴
plot(xa,ya,'ro',xb,yb,'ro','MarkerSize',12); % 画已测点和待测点
hold on; % 保持图像
plot ([xa,xb],[ya,yb], 'k','LineWidth',2);
grid on;
text(xa,ya,'A 点','Fontsize',16);text(xb,yb,'B 点','Fontsize',16); % 画直线段与标注点
plot([xa,xa], [ya,yb], 'b-.');plot([xa,xb],[yb,yb],'b-.');   % 画两虚线段
x0 = max(xa,xb); y0 = max(ya,yb); x2 = min(xa,xb); y2 = min(ya,yb);
axis([x2-0.1*x2,x0+0.1*x0,y2-0.1*y2,y0+0.1*y0]); axis('equal');%  确定坐标轴范围
xlabel('Y 轴');ylabel('X 轴'); % 标注横竖坐标轴
hold off; % 关闭图像

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


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



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


% --- Executes during object creation, after setting all properties.
function edit5_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit5 (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 edit6_Callback(hObject, eventdata, handles)
% hObject    handle to edit6 (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 edit6 as text
%        str2double(get(hObject,'String')) returns contents of edit6 as a double


% --- Executes during object creation, after setting all properties.
function edit6_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit6 (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 edit8_Callback(hObject, eventdata, handles)
% hObject    handle to edit8 (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 edit8 as text
%        str2double(get(hObject,'String')) returns contents of edit8 as a double


% --- Executes during object creation, after setting all properties.
function edit8_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit8 (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 edit13_Callback(hObject, eventdata, handles)
% hObject    handle to edit13 (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 edit13 as text
%        str2double(get(hObject,'String')) returns contents of edit13 as a double


% --- Executes during object creation, after setting all properties.
function edit13_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit13 (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 edit14_Callback(hObject, eventdata, handles)
% hObject    handle to edit14 (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 edit14 as text
%        str2double(get(hObject,'String')) returns contents of edit14 as a double


% --- Executes during object creation, after setting all properties.
function edit14_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit14 (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 edit15_Callback(hObject, eventdata, handles)
% hObject    handle to edit15 (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 edit15 as text
%        str2double(get(hObject,'String')) returns contents of edit15 as a double


% --- Executes during object creation, after setting all properties.
function edit15_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit15 (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 edit16_Callback(hObject, eventdata, handles)
% hObject    handle to edit16 (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 edit16 as text
%        str2double(get(hObject,'String')) returns contents of edit16 as a double


% --- Executes during object creation, after setting all properties.
function edit16_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit16 (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 edit9_Callback(hObject, eventdata, handles)
% hObject    handle to edit9 (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 edit9 as text
%        str2double(get(hObject,'String')) returns contents of edit9 as a double


% --- Executes during object creation, after setting all properties.
function edit9_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit9 (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 edit10_Callback(hObject, eventdata, handles)
% hObject    handle to edit10 (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 edit10 as text
%        str2double(get(hObject,'String')) returns contents of edit10 as a double


% --- Executes during object creation, after setting all properties.
function edit10_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit10 (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 edit11_Callback(hObject, eventdata, handles)
% hObject    handle to edit11 (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 edit11 as text
%        str2double(get(hObject,'String')) returns contents of edit11 as a double


% --- Executes during object creation, after setting all properties.
function edit11_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit11 (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 edit12_Callback(hObject, eventdata, handles)
% hObject    handle to edit12 (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 edit12 as text
%        str2double(get(hObject,'String')) returns contents of edit12 as a double


% --- Executes during object creation, after setting all properties.
function edit12_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit12 (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 pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% 计算距离 D2
ya2 = str2num(get(handles.edit10,'string')); 
xa2 = str2num(get(handles.edit9,'string')); 

yb2 = str2num(get(handles.edit12,'string')); 
xb2 = str2num(get(handles.edit11,'string'));

xab2 = xb2 - xa2;yab2 = yb2-ya2;
D2 = num2str(sqrt(yab2^2+xab2^2));
set(handles.edit16,'string',num2str(D2)); 

% 绘制图像
% axes(handles,axes1); % 打开坐标轴
plot(xa2,ya2,'ro',xb2,yb2,'ro','MarkerSize',12); % 画已测点和待测点
hold on; % 保持图像
plot ([xa2,xb2],[ya2,yb2], 'k','LineWidth',2);
grid on;
text(xa2,ya2,'A 点','Fontsize',16);text(xb2,yb2,'B 点','Fontsize',16); % 画直线段与标注点
plot([xa2,xa2], [ya2,yb2], 'b-.');plot([xa2,xb2],[yb2,yb2],'b-.');   % 画两虚线段
x02 = max(xa2,xb2); y02 = max(ya2,yb2); x22 = min(xa2,xb2); y22 = min(ya2,yb2);
axis([x22-0.1*x22,x02+0.1*x02,y22-0.1*y22,y02+0.1*y02]); axis('equal');%  确定坐标轴范围
xlabel('Y 轴');ylabel('X 轴'); % 标注横竖坐标轴
hold off; % 关闭图像

% 计算方位角
fwj = atan(yab2/xab2);
fwj = rad2deg(fwj);
if xab2>0&yab2>0
    fwj = fwj;
elseif xab2>0&yab2<0
    fwj = 360 + fwj;
elseif xab2<0&yab2<0
    fwj = 180 + fwj;
elseif xab2<0&yab2>0
    fwj = 180 + fwj;
elseif xab2 == 0&yab2>0
    fwj = 0;
elseif xab2 == 0&yab2<0
    fwj = 180;
elseif yab2 == 0&xab2<0
    fwj = 270;
elseif yab2 == 0&xab2>0
    fwj = 90;
end

du = fix(fwj); fen = fix((fwj - du)*60); miao = 60*((fwj - du)*60 - fen); % 计算方位角的度分秒数
du = num2str(du);fen = num2str(fen);miao = num2str(miao);
set(handles.edit13,'string',du);set(handles.edit14,'string',fen);set(handles.edit15,'string',miao); %  显示方位角的度分秒数

% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(findobj('style','edit'),'string','')%清空edit文本数据

  • 9
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
MATLAB GUI(图形用户界面)是一种用于编写和设计交互式应用程序的工具。编写坐标正反程序是指通过输入一定的坐标信息,然后根据程序逻辑进行计,得出相应的结果。以下是一个简单的用MATLAB GUI编写坐标正反程序的示例。 首先,我们需要创建一个GUI窗口,该窗口包含输入坐标的文本框和按钮,以及显示结果的文本框。我们可以使用MATLABGUIDE界面设计工具来创建GUI窗口。 接下来,我们需要为按钮的回调函数编写代码。当用户点击按钮时,程序会执行该回调函数。在回调函数中,我们需要获取用户输入的坐标,并进行相应的计。 例如,如果用户输入了直角坐标系中的一个点的坐标(x,y),我们可以通过勾股定理来计该点到原点的距离。 在回调函数中,我们可以使用MATLAB的预定义函数来进行计。首先,我们需要从输入文本框中获取用户输入的坐标,并将其转换为数值型。然后,我们可以使用勾股定理公式`distance = sqrt(x^2 + y^2)`来计距离。最后,我们将计结果显示在结果文本框中。 除了坐标的正向计(即通过给定的坐标结果),我们还可以实现坐标的反向计,即通过给定的结果计坐标。例如,如果用户输入了一个点到原点的距离,我们可以通过勾股定理反向计出该点的坐标。 在完成代码编写后,我们可以保存并运行GUI程序。用户可以在GUI窗口中输入坐标,并点击按钮获取计结果。计结果将会显示在结果文本框中。 总而言之,通过利用MATLAB GUI编写坐标正反程序,我们可以创建一个交互式的应用程序,方便用户进行坐标。用户可以输入坐标,点击按钮获取计结果,并在GUI窗口中显示结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

༺ Hiraeth ༻

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值