Access to the callback function of checkbox - matlab

I have some checkbox and a push button to select all of them.
Code of my button "Select all" :
for i = 1:5
set(sprintf('handles.check%d',i),'value', 1)
end
But, I would like to go to the checkbox callback function each time I select one of them. How can I do this ? Because for the moment, all my check box are selected when I push the button "Select all" but the code into each checkbxo is not executed. My code in a checkbox is :
function check1_Callback(hObject, eventdata, handles)
if(get(hObject ,'Value') == 1)
ind=1;
assignin('base','ind',ind);
end
end

Related

Can waitforbuttonpress be used with switch cases

I have used this piece of Code but it's not working. Can you help me where I am wrong here..
for i=1:10
keydown = waitforbuttonpress;
switch keydown
case'0'
disp(5);
case'1'
disp(6);
end
end
Thank you
You are using '0' and '1', when you should use 0 and 1.
'0' - char type '0' (ASCII value of '0' equals 48. double('0') = 48).
waitforbuttonpress documentation:
k = waitforbuttonpress blocks the caller's execution stream until the function detects that the user has clicked a mouse button or pressed a key while the figure window is active. The figure that is current when you call the waitforbuttonpress function is the only area in which users can press a key or click a mouse button to resume program execution. The return argument, k, can have these values:
0 if it detects a mouse button click
1 if it detects a key press
Modify your code as follows:
for i=1:10
keydown = waitforbuttonpress;
switch keydown
case 0
disp(5);
case 1
disp(6);
end
end

matlab's GUI KeyPressFcn

I am using the KeyPressFcn method for the edit box to test whether enter is pressed. I can use call_back but there is not event_data call_back function.
If I press one time on the button of Enter, than text doesn't rewrite, but if I double time on the button of Enter (speedly), than text rewrite.
What reasons this behaviour?
function WriteData(val, name, ind)
global solver;
switch ind
case {14, 15}
value = strcat('#(t)', val);
case 16
value = strcat('#(x)', val);
case {17, 18}
value = strcat('#(x,t)', val);
end
eval(strcat('solver.', name, ' = ', num2str(val) ) );
function edit1_KeyPressFcn(hObject, eventdata, handles)
val = get(hObject, 'String');
[~, ~, var] = GetActiveData(handles.listbox1);
ind = get(handles.listbox1, 'Value');
if (strcmp(eventdata.Key, 'return') )
WriteData(val, var, ind );
end
According to the documentation found in UIControl Properties (http://www.mathworks.com/help/matlab/ref/uicontrol-properties.html;jsessionid=49b9dc47d9f964ec95a4fe2cc9f3),
This callback function executes when the uicontrol object has focus and the user presses a key. If you do not define a function for this property, MATLAB passes key presses to the parent figure. Repeated key presses retain the focus of the uicontrol, and the function executes with each key press. If the user presses multiple keys at approximately the same time, MATLAB detects the key press for the last key pressed.
More simply put, the Callback will be called when you press enter the first time and the KeyPressFcn will be applied on the second press.

Terminating while loop with a pushbutton Matlab

I have a uitable and a function that returns the id of the item once the user clicks on its respective row. The id is also a global variable, as it is used over various functions.
The idea is that I create an array with all the user chosen items from the uitable, terminated when save playlist is clicked. Ex. if the user chooses items with id's 5, 7, 9 in succession and then clicks the 'save playlist' button, I want the array to hold
5 7 9
I thought that the best way to do this is using a while loop. The while loop should check if the save button has been clicked. I have a global variable that changes once the program goes into the 'save playlist' button callback function.
The problem is that once the user chooses an ID, the array keeps on iterating until the user chooses another ID, and will keep on iterating etc etc until the save button is clicked. Ex: the user clicks on ID 5, the array will record 55555555555555... recurring until user clicks on ex. ID 7: 555555555555557777777777, since the program keeps on looping and looping without pausing.
The code looks like this:
while (keeprunning)
idvec = [idvec id];
end
keeprunning is a global variable and is initialized with a value of 1 in the opening function. It is changed to 0 in the 'save playlist' callback function.
I thought to fix this problem by introducing a new global variable call it 'itemselected' that initializes in the opening function as 1 and changes to 1 again everytime the cell selection callback is called. After an ID is added to the array in the while loop, this variable resets to 0.
The idea is that the loop should iterate while the 'save playlist' button is not clicked (controlled by the keeprunning global variable) BUT the program should not let the while loop iterate until the user clicks on another row i.e. until the cell selection call back function is called again.
I need something like this but this doesn't work (infinite loop). Or else some other suggestion??
while (keeprunning || trackselected)
idvec = [idvec id];
trackselected = 0;
end
Thank you in advance
To record a history of the user selecting fields inside a uitable, do something like this:
function tbl_example
% Create the figure
fh = figure('Visible', 'off')
ud.selected_list = [];
d = gallery('integerdata',100,[10 3],0);
t = uitable(fh,'Data',d,'ColumnWidth',{50}, 'cellselectioncallback', #cell_history );
set( fh, 'visible', 'on' );
set( fh, 'userdata', ud );
return
function cell_history( h, event_data )
fh = get( h, 'Parent' )
ud = get( fh, 'userdata' );
indx = event_data.Indices;
ud.selected_list = [ ud.selected_list h.Data( indx(1), indx(2) ) ]
set( fh, 'userdata', ud );
return
Then, when you hit your "playlist" button, just read the userdata field in the parent figure handle.
I fixed this problem after stopping from thinking about it and doing something else; fascinatingly it came to me as a very simple method.
What I did was create a global array in the CellSelectionCallback. The program checks whether the program was initiated to create a playlist through the use of a boolean variable in the 'create a playlist' button callback, and in this case the global array is appended with the chosen row's item's id. Since the array is global it can be used over different functions. The array is flushed after 'save playlist' button is clicked.
It was as simple as that, however, sometimes simple is not that simple, especially after about 10 hours of seeing matlab code.

Close MATLAB GUI with a dialog

I'm doing a GUI with GUIDE and want to close the figure with a dialog.
I have a button with this
selection = questdlg('Close This Figure?',...
'Close Request Function',...
'Yes','No','Yes');
switch selection,
case 'Yes',
delete(gcf)
case 'No'
return
end
and it works fine but I want the main button to do the same thing.
I tryed to put this next to
function varargout = file_name(varargin)
...
but won't work. Any suggestions?
Use above code in a function say:
%Save in myclose_callback.m
function myclose_callback(src,evnt)
selection = questdlg('Close This Figure?',...
'Close Request Function',...
'Yes','No','Yes');
switch selection,
case 'Yes',
delete(gcf)
case 'No'
return
end
end
Then use:
%From other .m script or from command window
figure('CloseRequestFcn',#myclose_callback)
To make this as your default callback for all figures, use:
set(0,'DefaultFigureCloseRequestFcn',#myclose_callback)

MATLAB GUI Button with Keyboard Control

I have a button in a GUI. which the user presses to execute a Callback. But, I would like the user to be able to press the up arrow key instead of clicking to execute the callback.
EDIT: I am using GUIDE to make the GUI
Check out this thread:
http://www.mathworks.com/matlabcentral/answers/12034
Just slightly modifying the code from there to here (put the following in a file called testGUI.m
function testGUI
g = figure('KeyPressFcn', #keyPress)
MyButton = uicontrol('Style', 'pushbutton','Callback',#task);
function task(src, e)
disp('button press');
end
function keyPress(src, e)
switch e.Key
case 'uparrow'
task(MyButton, []);
end
end
end