Error in push button callback

14 views (last 30 days)
Aaron Smith
Aaron Smith on 31 Mar 2017
Commented: Rik on 27 Jul 2019
I have created a GUI window with a push button. When pushed, a folder selection window should be generated and then when a folder is selected, the code of my callback should be executed. I have another window with a push button and a code that carries out a different but similar function which works perfectly fine. My question is effectively to get a second pair of eyes that might be able to spot the root of the error. The callback code:
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
myFile = uigetdir('C:\Users\c13459232\Documents\MATLAB'); % Generate command window to choose a folder
if ~isdir(myFile) % if the directory is not a valid path
errorMessage = sprintf('Error: the following file does not exist: \n%s', myFile); % print this error message
uiwait(warndlg(errorMessage)); % block the execution of program and wait to resume
return;
end
f2d = fopen(fullfile(myFile,'temp_01.asc'),'w'); % open a file to have data written into (fullfile builds full file name from parts)
f1d = fopen(fullfile(myFile,'TEST_A.asc'),'r'); % open the file to me looked at ('r' means read file)
k = 0;
while ~feof(f1d) % while find end of file, the TEST_A file
str = fgetl(f1d); %fgetl reads TEST_A line by line and this is identified by str
if sscanf(str,'%d')==1 % if the data (read line by line) from a string is equal to one
k = k+1; % k should increase by one with every block
fclose(f2d); % close the file that is being written to
fnm = fullfile(myFile,sprintf('temp_%02d.asc',k)); %build a full file from parts that are being formatted into strings
f2d = fopen(fnm,'w'); %open the parts to be written
end
fprintf(f2d,'%s\n',str); % writes the blocks to the file
end
fclose(f1d); %close the original TEST_A file
fclose(f2d); %close the new file
The error:
Undefined function 'just_push' for input arguments of type 'char'.
Error in @(hObject,eventdata)just_push('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
I can't see where the char input is being specified as this code worked previously, before i added it to the push button callback.

Answers (2)

Guillaume
Guillaume on 31 Mar 2017
The part of the error you need to focus on is not the for input arguments of type 'char' . It is the undefined function 'just_push' part that is important.
Does that just_push callback function exist?
It's a bit unusual to have a callback function being given the name of another callback function as its first input argument, shouldn't that callback definition simply be:
@(hObject, eventdata) pushbutton1_Callback(hObject, eventdata, guidata(hObject))
or even simpler, since pushbutton1_Callback gets the hObject anyway it could query the guidata itself, and the callback definition could just be
@pushbutton1_Callback
with the definiton of pushbutton1_Callback:
function pushbutton1_Callback(hObject, eventdata)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
handles = guidata(hObject);
%... rest of the code
In any case, the error has nothing to do with the snippet of code you've posted.
  2 Comments
Aaron Smith
Aaron Smith on 31 Mar 2017
function varargout = just_push(varargin)
% JUST_PUSH MATLAB code for just_push.fig
% JUST_PUSH, by itself, creates a new JUST_PUSH or raises the existing
% singleton*.
%
% H = JUST_PUSH returns the handle to a new JUST_PUSH or the handle to
% the existing singleton*.
%
% JUST_PUSH('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in JUST_PUSH.M with the given input arguments.
%
% JUST_PUSH('Property','Value',...) creates a new JUST_PUSH or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before just_push_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to just_push_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 just_push
% Last Modified by GUIDE v2.5 31-Mar-2017 11:55:28
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @just_push_OpeningFcn, ...
'gui_OutputFcn', @just_push_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 just_push is made visible.
function just_push_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 just_push (see VARARGIN)
% Choose default command line output for just_push
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes just_push wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = just_push_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 pushbutton1.
function pushbutton1_Callback(hObject, eventdata)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
handles = guidata(hObject);
myFile = uigetdir('C:\Users\c13459232\Documents\MATLAB'); % Generate command window to choose a folder
if ~isdir(myFile) % if the directory is not a valid path
errorMessage = sprintf('Error: the following file does not exist: \n%s', myFile); % print this error message
uiwait(warndlg(errorMessage)); % block the execution of program and wait to resume
return;
end
f2d = fopen(fullfile(myFile,'temp_01.asc'),'w'); % open a file to have data written into (fullfile builds full file name from parts)
f1d = fopen(fullfile(myFile,'TEST_A.asc'),'r'); % open the file to me looked at ('r' means read file)
k = 0;
while ~feof(f1d) % while find end of file, the TEST_A file
str = fgetl(f1d); %fgetl reads TEST_A line by line and this is identified by str
if sscanf(str,'%d')==1 % if the data (read line by line) from a string is equal to one
k = k+1; % k should increase by one with every block
fclose(f2d); % close the file that is being written to
fnm = fullfile(myFile,sprintf('temp_%02d.asc',k)); %build a full file from parts that are being formatted into strings
f2d = fopen(fnm,'w'); %open the parts to be written
end
fprintf(f2d,'%s\n',str); % writes the blocks to the file
end
fclose(f1d); %close the original TEST_A file
fclose(f2d); %close the new fileile
I replaced the code with what you suggested and got this error
Undefined function 'just_push' for input arguments of type 'char'.
Error in @(hObject,eventdata)just_push('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
I'm not sure why the naming of the window is causing problems for the callback
Aaron Smith
Aaron Smith on 31 Mar 2017
I made a new window consisting of the same pushbutton and simply changed the name of the window from just_push to push_button and by adding my original callback, it works. I'm confused by how this could've caused a problem

Sign in to comment.


Beena Anand
Beena Anand on 27 Jul 2019
is there is any addones to install for push button callback?
  1 Comment
Rik
Rik on 27 Jul 2019
No, callback functions and uicontrol are part of native Matlab. Also, this is not an answer to this question. Please open your own question or write a comment instead.

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!