📄 location_gui.m
字号:
function varargout = location_gui(varargin)%location_gui函数名
% LOCATION_GUI M-file for location_gui.fig
% LOCATION_GUI, by itself, creates a new LOCATION_GUI or raises the existing
% singleton*.
%
% H = LOCATION_GUI returns the handle to a new LOCATION_GUI or the handle to
% the existing singleton*.
%
% LOCATION_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in LOCATION_GUI.M with the given input arguments.
%
% LOCATION_GUI('Property','Value',...) creates a new LOCATION_GUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before location_gui_OpeningFunction gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to location_gui_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 location_gui
% Last Modified by GUIDE v2.5 12-Jan-2006 11:18:01
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @location_gui_OpeningFcn, ...
'gui_OutputFcn', @location_gui_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin & isstr(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 location_gui is made visible.
function location_gui_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 location_gui (see VARARGIN)
% Choose default command line output for location_gui
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes location_gui wait for user response (see UIRESUME)
% uiwait(handles.figure1);GUI为图形界面设计
% --- Outputs from this function are returned to the command line.
function varargout = location_gui_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 during object creation, after setting all properties.
function length_edit_CreateFcn(hObject, eventdata, handles)
% hObject handle to length_edit (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 length_edit_Callback(hObject, eventdata, handles)
% hObject handle to length_edit (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 length_edit as text
% str2double(get(hObject,'String')) returns contents of length_edit as a double
% --- Executes during object creation, after setting all properties.
function width_edit_CreateFcn(hObject, eventdata, handles)
% hObject handle to width_edit (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 width_edit_Callback(hObject, eventdata, handles)
% hObject handle to width_edit (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 width_edit as text
% str2double(get(hObject,'String')) returns contents of width_edit as a double
% --- Executes on button press in ok.
function ok_Callback(hObject, eventdata, handles)
% hObject handle to ok (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%%按照场地均匀安排锚节点
%考虑测距误差为正负10%
%初始化节点及存放定位信息矩阵
node_num=200;%一般节点数量
%锚节点数量大约为一般节点数量的10%,依场地情况而定
length=100;%场地长度
width=100;%场地宽度
rangemax=15;%节点间最大通信距离
%从GUI获得数据
node_num=str2double(get(handles.node_edit,'String'));
length=str2double(get(handles.length_edit,'String'));
width=str2double(get(handles.width_edit,'String'));
[node_x,node_y]=creatnode(node_num,length,width);%生成普通节点
%开始生成锚节点位置,20个锚节点均匀分布
anchor_length=sqrt(length*width*10/100);
anchor_no=1;
for temp_width=1:(1+width/(anchor_length-7))
for temp_length=1:(1+length/anchor_length)
anchor_x(anchor_no)=anchor_length*(temp_length-1);
anchor_y(anchor_no)=(anchor_length-7)*(temp_width-1);
anchor_no=anchor_no+1;
end
end
anchor_num=anchor_no-1;%最后得到的锚节点数量
% 生成存放定位信息的矩阵
%纵坐标为到第一个锚节点的跳数,到第一个锚节点的距离和,到第二个锚节点的跳数……
%横坐标为所需定位节点的序号,这个序号与最初生成的node序号是一致的
locateinf=ones(2*anchor_num,node_num)*inf;
ok='初始化结束'
%% 察看初始节点状况
plot(anchor_x,anchor_y,'*',node_x,node_y,'*');
%% 程序开始
%对图进行广度先遍历
abcanchor=0;
abcnode=0;
for anchor_no=1:anchor_num
%初始化队列
abcanchor=abcanchor+1
queue=zeros(1,node_num);
front=1;
rear=1;
%
for node_no=1:node_num
%abcnode=abcnode+1
distance=dist(anchor_x(anchor_no),anchor_y(anchor_no),node_x(node_no),node_y(node_no));
if distance<=rangemax
locateinf(2*(anchor_no-1)+1,node_no)=1; %跳数
locateinf(2*anchor_no,node_no)=distance; %距离和
%将该点加入队列
queue(1,rear)=node_no;
rear=rear+1;
%
end
while(front<rear)
%从队列中取出元素,该点为known
known=queue(1,front);
front=front+1;
%
for node_no=1:node_num
distance=dist(node_x(known),node_y(known),node_x(node_no),node_y(node_no));
if distance<=rangemax
if locateinf(2*(anchor_no-1)+1,node_no)>(locateinf(2*(anchor_no-1)+1,known)+1) %在此可考虑增加采用距离和较短的算法
locateinf(2*(anchor_no-1)+1,node_no)=(locateinf(2*(anchor_no-1)+1,known)+1);
locateinf(2*anchor_no,node_no)=locateinf(2*anchor_no,known)+distance;
%将该点加入队列
queue(1,rear)=node_no;
rear=rear+1;
%
end
end
end
end
end
end
ok='完成图的广度先遍历'
%% 生成锚节点优先选取矩阵anchorchosen,将距离远的锚节点序号放在后面,即从好到坏排列
for node_no=1:node_num
node_no
for anchor_no=1:anchor_num
%构造temp矩阵
temp(anchor_no,1)=anchor_no;
temp(anchor_no,2)=locateinf((2*anchor_no),node_no);
end
%对temp矩阵进行排序处理
for ti=1:anchor_num-1
min=ti;
for tj=ti+1:anchor_num
if temp(tj,2)<temp(min,2)
min=tj;
end
end
if ti~=min
tempintemp1=temp(ti,1);
tempintemp2=temp(ti,2);
temp(ti,1)=temp(min,1);
temp(ti,2)=temp(min,2);
temp(min,1)=tempintemp1;
temp(min,2)=tempintemp2;
end
end
for anchor_no=1:anchor_num
anchorchosen(anchor_no,node_no)=temp(anchor_no,1);
end
end
ok='生成锚节点优先选取矩阵'
%% 改进算法,选取跳数少的锚节点进行定位,节点坐标存为(locate_near_x,locate_near_y)
locate_near_x=zeros(1,node_num);
locate_near_y=zeros(1,node_num);
for node_no=1:node_num
tempflag=0;
node_no
for chosen1=1:(anchor_num-2)
for chosen2=(chosen1+1):(anchor_num-1)
for chosen3=(chosen2+1):anchor_num
%[x,y]=barycenter(anchor_x(anchorchosen(chosen1,node_no)),anchor_y(anchorchosen(chosen1,node_no)),locateinf(2*anchorchosen(chosen1,node_no),node_no),anchor_x(anchorchosen(chosen2,node_no)),anchor_y(anchorchosen(chosen2,node_no)),locateinf(2*anchorchosen(chosen2,node_no),node_no),anchor_x(anchorchosen(chosen3,node_no)),anchor_y(anchorchosen(chosen3,node_no)),locateinf(2*anchorchosen(chosen3,node_no),node_no));
[x,y]=barycenter(anchor_x(anchorchosen(chosen1,node_no)),anchor_y(anchorchosen(chosen1,node_no)),locateinf(2*anchorchosen(chosen1,node_no),node_no),anchor_x(anchorchosen(chosen2,node_no)),anchor_y(anchorchosen(chosen2,node_no)),locateinf(2*anchorchosen(chosen2,node_no),node_no),anchor_x(anchorchosen(chosen3,node_no)),anchor_y(anchorchosen(chosen3,node_no)),locateinf(2*anchorchosen(chosen3,node_no),node_no));
if((isreal(x)==1)&&(isreal(y)==1))
locate_near_x(node_no)=x;
locate_near_y(node_no)=y;
tempflag=1;
end
if(tempflag==1) break; end
end
if(tempflag==1) break; end
end
if(tempflag==1) break; end
end
end
ok='定位完成'
%% 计算误差
% 距离差
difference_x=abs(locate_near_x-node_x);
difference_y=abs(locate_near_y-node_y);
%% 显示距离差
plot(difference_x,difference_y,'+');
% 得出距离差,放在矩阵difference中
for node_no=1:node_num
difference(node_no)=sqrt((difference_x(node_no))^2+(difference_y(node_no))^2);
end
%% 得到距离差大于值gatevalue的点,将节点序号存于badpoint中,badpoint_no为badpoint的数量
gatevalue=rangemax;
badpoint_no=1;
for node_no=1:node_num
if difference(node_no)>gatevalue
badpoint(badpoint_no)=node_no;
badpoint_no=badpoint_no+1;
end
end
badpoint_no=badpoint_no-1;
range_error=(mean(difference)/rangemax)*100
% 绘制node节点位置和计算出的位置,并用线段连接
plot(anchor_x,anchor_y,'*',node_x,node_y,'*',locate_near_x,locate_near_y,'+');
hold on;
for node_no=1:node_num
line([node_x(node_no),locate_near_x(node_no)],[node_y(node_no),locate_near_y(node_no)]);
end
s=['平均定位误差:',num2str(range_error),'%'];
title(s);
hold off;
%plot(anchor_x,anchor_y,'*',node_x,node_y,'*')
%hold on;
%plot(locate_near_x,locate_near_y,'.','MarkerSize',15,'Color','r');
%for node_no=1:node_num
% line([node_x(node_no),locate_near_x(node_no)],[node_y(node_no),locate_near_y(node_no)]);
%end
%hold off;
%门限值为最大通信距离,显示距离差大于门限值的节点个数
badpoint_no
%计算连通度,并显示结果
count=0;
for node_no=1:node_num
if(isnan(difference(node_no))~=1)
count=count+1;
end
end
connectivity=count/node_num;
connectivity
ok='全部完成'
% --- Executes during object creation, after setting all properties.
function node_edit_CreateFcn(hObject, eventdata, handles)
% hObject handle to node_edit (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 node_edit_Callback(hObject, eventdata, handles)
% hObject handle to node_edit (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 node_edit as text
% str2double(get(hObject,'String')) returns contents of node_edit as a double
% --- Executes during object creation, after setting all properties.
function anchor_edit_CreateFcn(hObject, eventdata, handles)
% hObject handle to anchor_edit (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 anchor_edit_Callback(hObject, eventdata, handles)
% hObject handle to anchor_edit (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 anchor_edit as text
% str2double(get(hObject,'String')) returns contents of anchor_edit as a double
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -