Undefined Variables Error - Using GUIDE
이전 댓글 표시
I'm trying to run some basic code with plotting through GUIDE, but I don't seem to be able to initialize, load data and define default variables correctly. Essentially I need to load a large dataset of 3,000x3,000 arrays (MCNPdata), then selects which one to use, then modifies with multipliers and plots the resulting array as a mesh or image.
Here are the errors I'm getting: Undefined function or variable 'detector_type'.
Error in DoseMapperGUI>runmapper_Callback (line 286) if detector_type==1
Error in gui_mainfcn (line 96) feval(varargin{:});
Error in DoseMapperGUI (line 42) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)DoseMapperGUI('runmapper_Callback',hObject,eventdata,guidata(hObject))
Error using drawnow Error while evaluating uicontrol Callback
Here's the code:
% --- Executes just before DoseMapperGUI is made visible.
function DoseMapperGUI_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 DoseMapperGUI (see VARARGIN)
% Load MCNP Pre-Calculated Mesh Data
% MCNPdata.mat must be located in the active directory
load('MCNPdata.mat');
% Choose default command line output for DoseMapperGUI
handles.output = hObject;
set(handles.source_type,'String','Single Point-source');
source_type = 1;
set(handles.scan_height,'String','100ft');
HeightMesh=HeightMesh100;
set(handles.source_strength,'String','1Rem/hr');
Source_Mesh=3.7E+10*3.5*HeightMesh;
set(handles.scan_speed,'String','12knots (20fps)');
scan_speed=1;
set(handles.detector_type,'String','3"x3" NaI');
detector_type=1;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes DoseMapperGUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = DoseMapperGUI_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 selection change in source_type.
function source_type_Callback(hObject, eventdata, handles)
% hObject handle to source_type (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Determine the selected data set.
STstr = get(handles.source_type, 'String');
STval = get(source,'Value');
% Set current data to the selected data set.
switch STstr{STval};
case 'Single Point-source'
source_type=1;
case 'Multiple Point-sources'
source_type=2;
case 'Gaussian Distribution'
source_type=3;
end
% Hints: contents = cellstr(get(hObject,'String')) returns source_type contents as cell array
% contents{get(hObject,'Value')} returns selected item from source_type
% --- Executes during object creation, after setting all properties.
function source_type_CreateFcn(hObject, eventdata, handles)
% hObject handle to source_type (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on selection change in source_strength.
function source_strength_Callback(hObject, eventdata, handles)
% hObject handle to source_strength (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
SSstr = get(handles.source_strength, 'String');
SSval = get(source,'Value');
% Set current data to the selected data set.
switch SSstr{SSval};
case '1Rem/hr'
Source_Mesh=3.7E+10*3.5*HeightMesh;
case '3Rem/hr'
Source_Mesh=3.7E+10*10*HeightMesh;
case '5Rem/hr'
Source_Mesh=3.7E+10*17*HeightMesh;
end
% Hints: contents = cellstr(get(hObject,'String')) returns source_strength contents as cell array
% contents{get(hObject,'Value')} returns selected item from source_strength
% --- Executes during object creation, after setting all properties.
function source_strength_CreateFcn(hObject, eventdata, handles)
% hObject handle to source_strength (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on selection change in detector_type.
function detector_type_Callback(hObject, eventdata, handles)
% hObject handle to detector_type (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
DTstr = get(handles.detector_type, 'String');
DTval = get(source,'Value');
% Set current data to the selected data set.
switch DTstr{DTval};
case '3"x3" NaI'
detector_type=1;
case '2"x2" NaI'
detector_type=2;
case '1"x1" NaI'
detector_type=3;
case 'GM Tube'
detector_type=4;
end
% Hints: contents = cellstr(get(hObject,'String')) returns detector_type contents as cell array
% contents{get(hObject,'Value')} returns selected item from detector_type
% --- Executes during object creation, after setting all properties.
function detector_type_CreateFcn(hObject, eventdata, handles)
% hObject handle to detector_type (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on selection change in scan_speed.
function scan_speed_Callback(hObject, eventdata, handles)
% hObject handle to scan_speed (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Scanstr = get(handles.scan_speed, 'String');
Scanval = get(source,'Value');
% Set current data to the selected data set.
switch Scanstr{Scanval};
case '12knots (20fps)'
scan_speed=1;
case '30knots (50fps)'
scan_speed=2;
case '59knots (100fps)'
scan_speed=3;
case '89knots (150fps)'
scan_speed=4;
case '118knots (200fps)'
scan_speed=5;
end
% Hints: contents = cellstr(get(hObject,'String')) returns scan_speed contents as cell array
% contents{get(hObject,'Value')} returns selected item from scan_speed
% --- Executes during object creation, after setting all properties.
function scan_speed_CreateFcn(hObject, eventdata, handles)
% hObject handle to scan_speed (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on selection change in scan_height.
function scan_height_Callback(hObject, eventdata, handles)
% hObject handle to scan_height (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
SHstr = get(handles.scan_height, 'String');
SHval = get(source,'Value');
% Set current data to the selected data set.
switch SHstr{SHval};
case '100ft'
HeightMesh=HeightMesh100;
case '300ft'
HeightMesh=HeightMesh300;
case '500ft'
HeightMesh=HeightMesh500;
case '700ft'
HeightMesh=HeightMesh700;
case '1,000ft'
HeightMesh=HeightMesh1000;
end
% Hints: contents = cellstr(get(hObject,'String')) returns scan_height contents as cell array
% contents{get(hObject,'Value')} returns selected item from scan_height
% --- Executes during object creation, after setting all properties.
function scan_height_CreateFcn(hObject, eventdata, handles)
% hObject handle to scan_height (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in plot_imagesc.
function plot_imagesc_Callback(hObject, eventdata, handles)
% hObject handle to plot_imagesc (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
imagesc(PoissonMesh);figure(gcf);colormap(hot);
% --- Executes on button press in plot_mesh.
function plot_mesh_Callback(hObject, eventdata, handles)
% hObject handle to plot_mesh (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
mesh(PoissonMesh);figure(gcf);
% --- Executes on button press in runmapper.
function runmapper_Callback(hObject, eventdata, handles)
% hObject handle to runmapper (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if detector_type==1
DetEff=0.3;
DetSA=4*pi*(1.5*2.54)^2;
DetScale=DetEff*DetSA;
DetBG=200;
elseif detector_type==2
DetEff=0.25;
DetSA=4*pi*(1*2.54)^2;
DetScale=DetEff*DetSA;
DetBG=100;
elseif detector_type==3
DetEff=0.2;
DetSA=4*pi*(.5*2.54)^2;
DetScale=DetEff*DetSA;
DetBG=30;
elseif detector_type==4
DetEff=0.02;
DetSA=3;
DetScale=DetEff*DetSA;
DetBG=1;
end
NetCPSMesh=Source_Mesh*DetScale;
if scan_speed==1
mesh=3000/20;
gridspace=20;
elseif scan_speed==2
mesh=3000/50;
gridspace=50;
elseif scan_speed==3
mesh=3000/100;
gridspace=100;
elseif scan_speed==4
mesh=3000/150;
gridspace=150;
elseif scan_speed==5
mesh=3000/200;
gridspace=200;
end
IdealScanMesh=zeros(mesh,mesh);
for i=1:1:mesh
for j=1:1:mesh
amax=i*gridspace;
amin=amax-gridspace+1;
b=(j*gridspace)-(gridspace/2);
CPSIntegral=NetCPSMesh(amin:amax,b);
Counts=trapz(CPSIntegral/gridspace);
IdealScanMesh(i,j)=Counts;
end
end
PoissonMesh=zeros(mesh,mesh);
for i=1:1:mesh
for j=1:1:mesh
count=IdealScanMesh(i,j)+DetBG;
RealCount=poissrnd(count);
PoissonMesh(i,j)=RealCount;
end
end
채택된 답변
추가 답변 (2개)
Babak
2012년 11월 27일
Before that line, where gives you the error, line # 286, add this line:
detector_type = handles.detector_type;
Please note that, the variable detector_type is not defined in the new function where you are trying to check, if detector_type==1 Therefore, you need to define it and getting it from the handles structure by
...
detector_type = handles.detector_type;
if detector_type==1
...
댓글 수: 1
Walter Roberson
2012년 11월 27일
No, handles.dector_type is a handle graphics handle of a uicontrol of style popup or listbox.
카테고리
도움말 센터 및 File Exchange에서 Graphics Object Properties에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!