Error in push button callback
14 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(2 个)
Guillaume
2017-3-31
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.
Beena Anand
2019-7-27
is there is any addones to install for push button callback?
1 个评论
Rik
2019-7-27
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!