Pushbutton callback function reacts only after second press when cell in uitable was selected beforehand

Hi,
I'm writing a switch rows function for a uitable which is started by a pushbutton in Matlab GUIDE. I've written the code which works fine though the problem is that I have to press the button twice as the corresponding callback function of the pushbutton is not started the first time.
I obviously need to select a cell in the table (I already got the indices via the cell selection callback function) and then press the pushbutton to do the actual switching. The callback function of the pushbutton should start immediately but it doesn't.
My Code is:
% --- Executes when selected cell(s) is changed in table_pathdata.
function table_pathdata_CellSelectionCallback(hObject, eventdata, handles)
% Read current cell indices
setappdata(hObject,'CurrentCell',eventdata.Indices);
Indices = getappdata(hObject,'CurrentCell');
assignin('base','Indices',Indices);
% --- Executes on button press in button_moverowup.
function button_moverowup_Callback(hObject, eventdata, handles)
'Teststring' % This String will only be displayed on second button press
% Switch code follows which is working fine (on second button press)
I hope someone can help with that problem. It might be very easy for experienced guys.

8 Commenti

Thomas - you may have to attach the complete code (m-file and fig-file) that may better illustrate the problem. Using the sample from above, I can select a cell in the table, and when I press the button, the 'TestString' immediately appears in the Command Window. This is with R2014a on a Mac.
As an aside, you do not have to save data as local variable in the base workspace. Just save the indices to the handles structure (using guidata) which the push button (and any other widget) will have easy access to. For example,
function table_pathdata_CellSelectionCallback(hObject, eventdata, handles)
% Read current cell indices
handles.selectedIndices = eventdata.Indices;
guidata(hObject,handles);
function button_moverowup_Callback(hObject, eventdata, handles)
% get the selected indices (if they exist)
if isfield(handles,'selectedIndices')
if ~isempty(handles.selectedIndices)
% do something with handles.selectedIndices
% finished with the selectedIndices so clear it
handles.selectedIndices = [];
guidata(hObject,handles);
end
end
The above is just a suggestion.
Hi, Geoff, it won't be possible to add the original files here but I attached a core program with which the problem can be replicated. If you select a cell and then press the button (which changes the "NaN"s to "1"s) the first hit won't give any reaction whereas the second hit will do. I think I would follow up on Image Analyst's opinion about the focus (see below).
P.S.: Thanks for your suggestion regarding the direct use of handles, not through workspace variables. I will look into that. :)
Hi Thomas - I can run your code, select a cell (so that it is all blue, not just the text), press the button and the cell contents (for each cell) changes to one. I do not have to press the button twice.
In the Command Window, check your java version
version -java
Mine (for R2014a on a Mac with OS X 10.8.5) is
Java 1.7.0_11-b21 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
I'm not suggesting that you change versions (if yours is different) because that could be risky and I don't know what the repercussions would be. I'm just curious as to why I can run this example from the command line or through GUIDE and not have a problem.
I also checked this on a PC with R2014a and with the same version of java, and it worked fine - I could not reproduce the error. Same for R2013a with java 1.6.0_17-b04, again on the same PC.
What version of MATLAB are you using?
I'm running Matlab 2013a 32bit Student Version on Windows 7. Java version is
Java 1.6.0_17-b04 with Sun Microsystems Inc. Java HotSpot(TM) Client VM mixed mode
Should I go for Oracle Java instead? Didn't know Matlab was using Java.
On another note ... my cells are not blue when selected but grey'ish (same as the window background color itself).
The java version for my R2013a is no different than yours for your version of MATLAB. I think that Image Analyst has the best solution/amswer for this. Though why it works in my environment is beyond me..
So, unfortunately, still no solution. Thanks anyway for your time.
I seem to be having this problem as well but I am using Java 1.6.0_65-b14-462-11M4609 with Apple Inc. Java HotSpot™ 64-Bit Server VM mixed mode
I have two different versions of my GUI the callback for a pushbutton is the same in both versions, but in one it works on the first click and in the other it takes two clicks..not sure why?

Accedi per commentare.

Risposte (1)

I've seen this before. When my user enters some text into a cell of a uitable, and I want to do something in a pushbutton callback, the first push of the pushbutton doesn't do anything and they have to push/click the pushbutton a second time to do the callback of the push button. So I have to have the string of my pushbutton say "OK (Click twice when done)". I'm not sure what's going on but it's like the first click of the pushbutton merely steps editing of the uitable and changes focus from the table to the pushbutton, like it's just ending the editing session of the table or something. Sorry, I don't know of a fix but I'd like to.

Categorie

Scopri di più su Environment and Settings in Centro assistenza e File Exchange

Prodotti

Richiesto:

il 27 Ago 2014

Commentato:

il 9 Set 2014

Community Treasure Hunt

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

Start Hunting!

Translated by