📄 makearray.m
字号:
%---------------------------------------------------------------
% makearray.m (last updated 5/19/2005)
% written by Chunwei Jethro Lam, UIUC, jethrolam@gmail.com
%
% A MATLAB GUI that allows user to deploy sensors in a 2-D field with
% mouse clicks. Useful in sensor network and antenna array simulations.
%
% Location data is saved in loc_data.mat as a Nx2 vector where
% N is the number of sensor. First sensor is at (0,0).
%---------------------------------------------------------------
function varargout = makearray(varargin)
% MAKEARRAY M-file for makearray.fig
% MAKEARRAY, by itself, creates a new MAKEARRAY or raises the existing
% singleton*.
%
% H = MAKEARRAY returns the handle to a new MAKEARRAY or the handle to
% the existing singleton*.
%
% MAKEARRAY('Property','Value',...) creates a new MAKEARRAY using the
% given property value pairs. Unrecognized properties are passed via
% varargin to makearray_OpeningFcn. This calling syntax produces a
% warning when there is an existing singleton*.
%
% MAKEARRAY('CALLBACK') and MAKEARRAY('CALLBACK',hObject,...) call the
% local function named CALLBACK in MAKEARRAY.M with the given input
% arguments.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @makearray_OpeningFcn, ...
'gui_OutputFcn', @makearray_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 makearray is made visible.
%----------------------------------------------------
function makearray_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 unrecognized PropertyName/PropertyValue pairs from the
% command line (see VARARGIN)
% Initialize
handles.loc = zeros(1,2);
handles.N=1;
handles.tokenN=2; % default action is ADD
set(handles.listbox,'Value',handles.N+1);
grid on; axis([-10 10 -10 10]);
% Display sensors and Update listbox
displaymap(handles)
updatelistbox(handles)
% Add ButtonDnFcn to map
set(handles.map,'ButtonDownFcn','makearray(''AddEditSensor'',gcbo,[],guidata(gcbo))');
% Choose default command line output for makearray
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes makearray wait for user response (see UIRESUME)
% uiwait(handles.figure1);
%----------------------------------------------------
% --- Convert loc into listbox message and display it
%----------------------------------------------------
function updatelistbox(handles)
% handles structure with handles and user data (see GUIDATA)
lbmsg = strcat(num2str((1:handles.N)'),' (',num2str(handles.loc,3),')');
lbmsg = strvcat(lbmsg,'Add') ;
set(handles.listbox,'String',lbmsg);
%----------------------------------------------------
% --- Display loc on map
%----------------------------------------------------
function displaymap(handles)
% handles structure with handles and user data (see GUIDATA)
cla; hold on;
plot(handles.loc(1:handles.N,1),handles.loc(1:handles.N,2),'bo');
if handles.tokenN <= handles.N
plot(handles.loc(handles.tokenN,1),handles.loc(handles.tokenN,2),'ro');
end
%----------------------------------------------------
% --- Perform ADD or EDIT
%----------------------------------------------------
function AddEditSensor(hObject,eventdata,handles)
% Get mouse click position from map
pt = get(gca,'CurrentPoint');
px = pt(1,1); py = pt(1,2);
if handles.tokenN > handles.N
% Add sensor
handles.N = handles.N+1;
handles.loc(handles.N,:)=[px py];
% next action is ADD by default
handles.tokenN = handles.tokenN+1;
set(handles.listbox,'Value',handles.N+1);
else
% Edit sensor indexed by tokenN, first sensor cannot be edited
if handles.tokenN~=1
handles.loc(handles.tokenN,:)=[px py];
end
end
% Display map and Update listbox
displaymap(handles);
updatelistbox(handles);
% Update handles structure
guidata(hObject, handles);
%----------------------------------------------------
% --- Outputs from this function are returned to the command line.
%----------------------------------------------------
function varargout = makearray_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
%----------------------------------------------------
% --- Executes during object creation, after setting all properties.
%----------------------------------------------------
function listbox_CreateFcn(hObject, eventdata, handles)
% hObject handle to listbox (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox 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 selection change in listbox.
%----------------------------------------------------
function listbox_Callback(hObject, eventdata, handles)
% hObject handle to listbox (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Change tokenN according to the selected listbox item
handles.tokenN= get(hObject,'Value');
% Update listbox
displaymap(handles);
% Update handles structure
guidata(hObject, handles);
%----------------------------------------------------
% --- Executes on button press in save.
%----------------------------------------------------
function save_Callback(hObject, eventdata, handles)
% hObject handle to save (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% save handles.loc to loc_data.mat
loc_data = handles.loc;
save loc_data loc_data;
%----------------------------------------------------
% --- Executes on button press in delete.
%----------------------------------------------------
function delete_Callback(hObject, eventdata, handles)
% hObject handle to delete (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Delete sensor indexed by tokenN
if (handles.tokenN ~=1)&&(handles.tokenN<=handles.N)
handles.loc(handles.tokenN:(handles.N-1),:) =...
handles.loc((handles.tokenN+1):handles.N,:);
handles.loc(handles.N,:)=[];
handles.N = handles.N-1;
% Display map and Update listbox
displaymap(handles);
updatelistbox(handles);
% Update handles structure
guidata(hObject, handles);
end
%------------------------------------------
% --- Executes on button press in load.
%------------------------------------------
function load_Callback(hObject, eventdata, handles)
% hObject handle to load (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% load handles.loc from loc_data.mat
load loc_data loc_data;
handles.loc = loc_data;
handles.N = size(loc_data,1);
% next action is ADD by default
handles.tokenN = handles.N+1;
set(handles.listbox,'Value',handles.N+1);
% Display map and Update listbox
displaymap(handles);
updatelistbox(handles);
% Update handles structure
guidata(hObject, handles);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -