How to solve Reference to non-existent field 'a '. Error

89 views (last 30 days)
this my code is :
function varargout = MyTab3TestNew(varargin)
%{
HOW TO CREATE A NEW TAB
1. Create or copy PANEL and TEXT objects in GUI
2. Rename tag of PANEL to "tabNPanel" and TEXT for "tabNtext", where N
- is a sequential number.
Example: tab3Panel, tab3text, tab4Panel, tab4text etc.
3. Add to Tab Code - Settings in m-file of GUI a name of the tab to
TabNames variable
Version: 1.0
First created: January 18, 2016
Last modified: January 18, 2016
Author: WFAToolbox (http://wfatoolbox.com)
%}
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @MyTab3TestNew_OpeningFcn, ...
'gui_OutputFcn', @MyTab3TestNew_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 MyTab3TestNew is made visible.
function MyTab3TestNew_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 MyTab3TestNew (see VARARGIN)
% Choose default command line output for MyTab3TestNew
handles.output = hObject;
%% Tabs Code
% Settings
TabFontSize = 10;
TabNames = {'Preprocess', 'Cropping'};
% FigWidth = 0.265; % the original
FigWidth = 0.700;
% Figure resize
set(handles.SimpleOptimizedTab,'Units','normalized')
pos = get(handles. SimpleOptimizedTab, 'Position');
set(handles. SimpleOptimizedTab, 'Position', [pos(1) pos(2) pos(3) pos(4)]); % % figWidth was changed to pos(3)
%figure color
set(gcf,'Color',[0.31 0.31 0.31]); % new modified
% Tabs Execution
handles = TabsFun(handles,TabFontSize,TabNames);
%tabs color
set(handles.tab1Panel,'BackgroundColor',[1 0 1]);% new modified
% set(handles.tab2Panel,'BackgroundColor',[1 0 1]);% new modified
% set(handles.tab3Panel,'BackgroundColor',[1 0 1]);% new modified
% set(handles.tab4Panel,'BackgroundColor',[1 0 1]);% new modified
% Update handles structure
guidata(hObject, handles);
% set the button next to off
set(handles.btnNext1,'enable','off');
% UIWAIT makes MyTab3TestNew wait for user response (see UIRESUME)
% uiwait(handles.SimpleOptimizedTab);
% --- TabsFun creates axes and text objects for tabs
function handles = TabsFun(handles,TabFontSize,TabNames)
% Set the colors indicating a selected/unselected tab
% set(handles.tab1Panel,'BackgroundColor', [1 0 1]); % not work
handles.selectedTabColor=get(handles.tab1Panel,'BackgroundColor');
% handles.selectedTabColor=get(handles.tab2Panel,'BackgroundColor'); % new modified
% handles.selectedTabColor=get(handles.tab3Panel,'BackgroundColor'); % new modified
% handles.selectedTabColor=get(handles.tab4Panel,'BackgroundColor'); % new modified
handles.unselectedTabColor=handles.selectedTabColor-0.1;
% Create Tabs
TabsNumber = length(TabNames);
handles.TabsNumber = TabsNumber;
TabColor = handles.selectedTabColor;
for i = 1:TabsNumber
n = num2str(i);
% Get text objects position
set(handles.(['tab',n,'text']),'Units','normalized')
pos=get(handles.(['tab',n,'text']),'Position');
% Create axes with callback function
handles.(['a',n]) = axes('Units','normalized',...
'Box','on',...
'XTick',[],...
'YTick',[],...
'Color',TabColor,...
'Position',[pos(1) pos(2) pos(3) pos(4)+0.01],...
'Tag',n,...
'ButtonDownFcn',[mfilename,'(''ClickOnTab'',gcbo,[],guidata(gcbo))']);
% Create text with callback function
handles.(['t',n]) = text('String',TabNames{i},...
'Units','normalized',...
'Position',[pos(3),pos(2)/2+pos(4)],...
'HorizontalAlignment','left',...
'VerticalAlignment','middle',...
'Margin',0.001,...
'FontSize',TabFontSize,...
'Backgroundcolor',TabColor,...
'Tag',n,...
'ButtonDownFcn',[mfilename,'(''ClickOnTab'',gcbo,[],guidata(gcbo))']);
TabColor = handles.unselectedTabColor;
end
% Manage panels (place them in the correct position and manage visibilities)
set(handles.tab1Panel,'Units','normalized')
pan1pos=get(handles.tab1Panel,'Position');
% set(handles.tab1Panel,'Color','red'); % New modified
set(handles.tab1text,'Visible','off')
for i = 2:TabsNumber
n = num2str(i);
set(handles.(['tab',n,'Panel']),'Units','normalized')
set(handles.(['tab',n,'Panel']),'Position',pan1pos)
set(handles.(['tab',n,'Panel']),'Visible','off')
set(handles.(['tab',n,'text']),'Visible','off')
end
% --- Callback function for clicking on tab
function ClickOnTab(hObject,~,handles)
m = str2double(get(hObject,'Tag'));
for i = 1:handles.TabsNumber;
n = num2str(i);
if i == m
set(handles.(['a',n]),'Color',handles.selectedTabColor)
set(handles.(['t',n]),'BackgroundColor',handles.selectedTabColor)
set(handles.(['tab',n,'Panel']),'Visible','on')
else
set(handles.(['a',n]),'Color',handles.unselectedTabColor)
set(handles.(['t',n]),'BackgroundColor',handles.unselectedTabColor)
set(handles.(['tab',n,'Panel']),'Visible','off')
end
end
% --- Outputs from this function are returned to the command line.
function varargout = MyTab3TestNew_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 btnInput.
function btnInput_Callback(hObject, eventdata, handles)
% hObject handle to btnInput (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global originalImage;
global grayImage;
global binaryImage;
global areaOpenImage;
% global OriginalPathName;
% [FileName,PathName]=uigetfile({'*.bmp;*.jpg;*.png'},' Choose an Aksara Document Image');
[FileName,PathName]=uigetfile('D:\pcd\tester\TemplateMatching\Input dokumen batak\dokumen karo\*.jpg',' Choose an Aksara Document Image');
%
if isequal(FileName,0)
errorMessage = sprintf('Error: %s does not exist in the search path folders.', FileName);
uiwait(warndlg(errorMessage));
return;
else
fullFileName = fullfile(PathName,FileName);
originalImage = imread(fullFileName);
% set the path name to editTextPath
set(handles.editTextPath,'string',fullFileName);
grayCheck = size(originalImage,3);
if grayCheck > 1
axes(handles.axes1);
imshow(originalImage, []);
%grayscaling
grayImage = rgb2gray(originalImage);
pause(0.05);
axes(handles.axes2);
imshow(grayImage, []);
%binarizing
binaryImage = ~im2bw(grayImage, graythresh(grayImage));
pause(0.05);
axes(handles.axes3);
imshow(binaryImage, []);
%area opening
areaOpenImage = bwareaopen(binaryImage, 24);
pause(0.05);
axes(handles.axes4);
imshow(areaOpenImage, []);
set(handles.btnNext1,'enable','on');
elseif length(unique(originalImage))>2 % is this grayscaled or not
% show as original
axes(handles.axes1);
imshow(originalImage, []);
%show as grayscaled
axes(handles.axes2);
imshow(grayImage, []);
%binarizing
binaryImage = ~im2bw(originalImage, graythresh(originalImage));
pause(0.05);
axes(handles.axes3);
imshow(binaryImage, []);
%area opening
areaOpenImage = bwareaopen(binaryImage, 24);
pause(0.05);
axes(handles.axes4);
imshow(areaOpenImage, []);
set(handles.btnNext1,'enable','on');
else
%show as original image
axes(handles.axes1);
imshow(originalImage, []);
%show as binary Image
axes(handles.axes3);
imshow(originalImage, []);
binaryImage = originalImage;
%area opening
areaOpenImage = bwareaopen(binaryImage, 24);
pause(0.05);
axes(handles.axes4);
imshow(areaOpenImage, []);
set(handles.btnNext1,'enable','on');
end
end
% --- Executes on button press in btnNext1.
function btnNext1_Callback(hObject, eventdata, handles)
% hObject handle to btnNext1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.(['a',2]),'Color',handles.selectedTabColor)
set(handles.(['t',2]),'BackgroundColor',handles.selectedTabColor)
set(handles.(['tab',2,'Panel']),'Visible','on')
function editTextPath_Callback(hObject, eventdata, handles)
% hObject handle to editTextPath (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 editTextPath as text
% str2double(get(hObject,'String')) returns contents of editTextPath as a double
% --- Executes during object creation, after setting all properties.
function editTextPath_CreateFcn(hObject, eventdata, handles)
% hObject handle to editTextPath (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 btnClear.
function btnClear_Callback(hObject, eventdata, handles)
% hObject handle to btnClear (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% arrayfun(@cla,findall(0,'type','axes')); % to clear all axeses
axes(handles.axes1);
cla('reset');
axes(handles.axes2);
cla('reset');
axes(handles.axes3);
cla('reset');
axes(handles.axes4);
cla('reset');
set(handles.editTextPath,'String',''); % to clear editTextPath
clear clearvars; % to clear all variables in workspace
after i ran this code i got this error :
Reference to non-existent field 'a'.
Error in MyTab3TestNew>btnNext1_Callback (line 261)
set(handles.(['a',2]),'Color',handles.selectedTabColor)
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in MyTab3TestNew (line 38)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)MyTab3TestNew('btnNext1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
i just want to open 'tab2Panel' tag when 'btnNext1' is clicked.
please help me to solve this error.
thanks before.
  2 Comments
Stephen23
Stephen23 on 3 Feb 2019
@Bachtiar Muhammad Lubis: please show us the complete error message. This means all of the red text.
Bachtiar Muhammad Lubis
Bachtiar Muhammad Lubis on 4 Feb 2019
@Stephen Cobeldick : That was my complete error message sir. Please help me sir

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 3 Feb 2019
It's nteresting that you get a 'non existent field' error. Newer versions of matlab throw an 'invalid field name' error instead which is more accurate.
It's never a good idea to number variable names or field names. Always use indexing instead. Particularly, if you don't know how to generate said variable of field names properly as is the case here. There is a big difference between ['a' 2] and ['a' '2']. The former is the letter 'a' followed by control character 2. This is not a valid field name, although in R2015b it appears matlab simply ignore the control character and you end up with just 'a. The latter is the char array 'a2'.
So that
set(handles.(['a',2]), ...
should have been:
set(handles.(['a', '2']), ...
or simply
set(handles.a2, ...
However, as said don't number field names, so I'd recommend that whenver you use a1, a2, etc. (or t1, t2, etc.) as field names, you use a(1), a(2), etc instead. This would require a fair amount of change to your code but ultimately would make it a lot simpler.
  3 Comments
Guillaume
Guillaume on 4 Feb 2019
No, a1 and a(1) are two different things. a1 is a scalar field with name a1. a(1) is the first element of array field named a. I would recommend that you use array instead of numbering your field names.
This way, instead of
for i = 1:handles.TabsNumber;
n = num2str(i);
if i == m
set(handles.(['a',n]),'Color',handles.selectedTabColor)
else
set(handles.(['a',n]),'Color',handles.unselectedTabColor)
end
end
you would have
for i = 1:handles.TabsNumber;
if i == m
set(handles.a(i),'Color',handles.selectedTabColor)
else
set(handles.a(i),'Color',handles.unselectedTabColor)
end
end
and so on, for the other fields. You no longer need to construct field names that way.
Bachtiar Muhammad Lubis
Bachtiar Muhammad Lubis on 5 Feb 2019
you are right sir, thank you for the advice sir. you are amazing

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products


Release

R2015b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!