?? 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 + -