Inputdlg in MATLAB - matlab

I have a figure window on MATLAB. I want the user to type in his answer on that figure window. The code I am using is :
> prompt = {'Your Age: '}
> dlg_title = 'Bio data'
> answer = inputdlg(prompt,dlg_title)'
This is the figure which I am getting.
My Questions :
1) How to make the age, which I type in this dialogue box, appear up at a specific position ON my figure window once I click on "ok" button of this dialogue box.
2) How to put up a customized background on this dialogue box.
3) How to get the user input ON the figure window without the dialogue box. like shown in the picture given bellow : (so that answer is typed on the horizontal line and vertical line is the cursor)

This might be close to what you want,
function age = AgeDB()
f = figure;
set(f,'Position',[200 350 350 150],'Color',[.4 .6 .4],'MenuBar','none',...
'Name','Bio data','Visible','off');
bc = [.4 .6 .4];
ht = uicontrol('Style','text','Position',[30 80 160 40],...
'String','Your Age:','FontSize',20,'FontWeight','bold',...
'BackgroundColor',bc,'ForegroundColor','w');
he = uicontrol('style','edit','Position', [200 80 120 40],...
'BackgroundColor',bc,'FontSize',20,'FontWeight','bold',...
'ForegroundColor','w','Callback',{#Age_Callback});
hp = uicontrol('Style', 'pushbutton', 'String', 'Ok',...
'Position', [150 10 50 20],...
'Callback', 'close');
movegui(f,'center')
set(f,'Visible','on')
waitfor(he)
function Age_Callback(hObject,eventdata)
age = str2double(get(hObject,'string'));
end
end

Related

How to get handle to active object MATLAB GUI

Im trying to create a calendar using MATLAB GUI.
I have two Edit Text objects - edittext1 and edittext2.
I want to do this one:
I put cursor at edittext1 then select date at calendar and it pits into text field of edittext1.
And the same for edittext2: if I put cursor into edittext2 and select date it puts into edittext2 Edit Text.
I know I can use callback for calendar this way.
Question:
How can I put into Callback function handler to ACTIVE edit text object?
How to get handle to object where cursor is now?
About the focus question, there is no active text box when you click a date on the java calendar, because the active component at this time is the java calendar.
To know which text box was active last, you simply need to keep track of it. One way is to add a callback to the edit box, which will update a variable (stored in the appdata) with the handle of the latest active text box.
Armed with that, the callback of the calendar will just retrieve the date, then place it in the last active text box.
Note: The ButtonDownFcn event of the text box will only fire on left and right click if the text box 'enable' property is 'off' or 'inactive'. (if it is 'on', then only the right click is detected). That is why I declared the text boxes as inactive. That does not prevent you to update the text programmatically so I didn't think it was a problem.
Code for testcalendar.m:
function testcalendar
handles.f = figure;
commonEditProperties = {'Style', 'edit', 'String', '', ...
'Units', 'Normalized', ...
'Enable','inactive' , ...
'callback',#EditBoxFcn , ...
'ButtonDownFcn',#EditBoxFcn } ;
handles.ledit = uicontrol( commonEditProperties{:}, 'Position', [0.1 0.1 0.3 0.1], 'Tag','ledit' );
handles.redit = uicontrol( commonEditProperties{:}, 'Position', [0.5 0.1 0.3 0.1], 'Tag','redit' );
% preallocate a variable to hold the active text box handle
setappdata(handles.f,'activeTextBox',[]) ;
com.mathworks.mwswing.MJUtilities.initJIDE;
% Put calendar to my figure
handles.jPanel = com.jidesoft.combobox.DateChooserPanel;
[handles.hPanel,handles.hContainer] = javacomponent(handles.jPanel,[100,100,200,200], handles.f);
juiFunHandle = handle(handles.jPanel, 'CallbackProperties');
set(juiFunHandle, 'MousePressedCallback', ...
#(src, evnt)CellSelectionCallback(src, evnt, handles));
set(juiFunHandle, 'KeyPressedCallback', ...
#(src, evnt)CellSelectionCallback(src, evnt, handles));
% store gui handles in application data
guidata(handles.f , handles)
end
function EditBoxFcn(hobj,~)
handles = guidata(hobj) ;
ActiveTextBox = get(hobj,'Tag') ;
setappdata( handles.f , 'activeTextBox', handles.(ActiveTextBox) ) ;
end
function CellSelectionCallback(~, ~, handles)
% retrieve the handle of the active text box
ActiveTextBox = getappdata(handles.f,'activeTextBox') ;
% assign a default active text box if none was selected before
if isempty(ActiveTextBox) ; ActiveTextBox = handles.ledit ; end
numRetry = 10 ;
for k=1:numRetry
pause(0.1)
dateString = char( javaMethodEDT('getSelectedDate', handles.jPanel) ) ;
if ~isempty(dateString) ; break ; end
end
set(ActiveTextBox , 'String' , dateString ) ;
end
See it in action:
Edit
There is no pure Matlab way to have your Matlab edit box fully editable an reacting (firing an event) to a single click of any mouse button.
You can get this functionality by using the text box underlying java object. This java object exposes a lot of events and you can just pick the one you need.
The catch:
To get the handle of the underlying java object, you need to use the almighty findjobj utility by Yair Altman. You can download the latest version from the file exchange here: findjobj
Once you have that saved in your Matlab path, just replace the few first line of code defining the edit boxes given in the example above by:
commonEditProperties = {'Style', 'edit', 'String', '', 'Units', 'Normalized', 'Enable','on' } ;
handles.ledit = uicontrol( commonEditProperties{:}, 'Position', [0.1 0.1 0.3 0.1] );
handles.redit = uicontrol( commonEditProperties{:}, 'Position', [0.5 0.1 0.3 0.1] );
% preallocate a variable to hold the active text box handle
setappdata(handles.f,'activeTextBox',[]) ;
% Find the java underlying object for the text boxes
ledit = findjobj(handles.ledit) ;
redit = findjobj(handles.redit) ;
% assign a callback to the java object (which CAN detect single click)
set(ledit,'MouseClickedCallback',#(h,e) setappdata( handles.f , 'activeTextBox', handles.ledit ) ) ;
set(redit,'MouseClickedCallback',#(h,e) setappdata( handles.f , 'activeTextBox', handles.redit ) ) ;
And you can completely comment or remove the sub-function EditBoxFcn as the callback action is done directly.

Create slider with dynamic range based on user input matlab gui

Hi I have a quick question on how to solve this issue. Basically I would like to take the inputs from two edit text boxes and specify them as the min and max of the sliders range. I don't have a problem doing this but as soon as the min is entered the slider disappears because the min is now above the default slider value, which is 0. I understand that the error is because the value is no longer in the range of the min and max, and I want to fix this by updating the value to be above the min in the call back function for the min/max input text boxes. Is there a way I can update the default value to be above min so I can get past this error and actually use the slider?
Warning: 'slider' control cannot have a 'Value' outside of 'Min'-'Max' range
Control will not be rendered until all of its parameter values are valid
Here is what I have tried to do in the call back to the edit box that gets user input for the minimum for the slider:
function input_min_Callback(hObject, eventdata, handles)
value_min=str2double(get(hObject, 'String'));
if value_min > sliderValue_default
set(handles.Input_Transverse_Shear_Layer1, 'Value', value_min+1);
set(handles.Input_Transverse_Shear_Layer1, 'Min', value_min);
end
Any help would be greatly appreciated!
Thanks
Your code looks fine to me. You seem to be missing a guidata(hObject,handles)at the end to update the guidata so this could be the problem (unless it's there but you did not include it in your above snippet).
In any case here is a bit of code that looks very much like yours and works fine. Try it out so you might see what is wrong with yours...
function UpdateSliderMin(~)
clc
clear
close all
%// Create GUI elements and set default slide value
hFig = figure('Position',[200 200 200 300]);
sliderValue_default = 0;
handles.Slider = uicontrol('style', 'Slider', 'Min', -5, 'Max', 10, 'Value',sliderValue_default, 'Units','normalized','position', [0.08 0.3 0.08 0.6], 'callback', #(s,e) SliderCbk);
handles.Text_min = uicontrol('Style','text','String','Min','position', [60 230 40 20]);
handles.Edit_min = uicontrol('Style','edit','String',num2str(get(handles.Slider,'min')),'position', [100 230 40 20],'Callback',#(s,e) MinCallback);
handles.Text_max = uicontrol('Style','text','String','Max','position', [60 180 40 20]);
handles.Edit_max = uicontrol('Style','edit','String',num2str(get(handles.Slider,'max')),'position', [100 180 40 20]);
handles.Text_val = uicontrol('Style','text','String','value','position', [60 130 40 20]);
handles.Edit_val = uicontrol('Style','edit','String',num2str(get(handles.Slider,'value')),'position', [100 130 40 20]);
guidata(hFig,handles)
%// Callback of the edit box for the min value
function MinCallback
value_min=str2double(get(handles.Edit_min, 'String'));
if value_min > sliderValue_default
set(handles.Slider, 'Value', value_min+1);
set(handles.Slider, 'Min', value_min);
set(handles.Edit_val,'String',get(handles.Slider, 'Value'));
end
guidata(hFig,handles)
end
%// Slider callback. Just to check the value is updated correctly
function SliderCbk
CurrentValue = get(handles.Slider,'Value');
set(handles.Edit_val,'String',num2str(CurrentValue));
guidata(hFig,handles)
end
end
Screenshot of the initial GUI:
And after setting the minimum value to 5, which is above the current value of the slider:
the current value is updated to 6 as expected.
Hope that helps!

Manage multiple callbacks

I have made a user interface with many edit text boxes created in loops, now I want to store the user inputs in a variable with callback.
For example consider this code,
function p = myfun()
f = figure;
set(f,'Position',[200 350 250 150],'Color',[.4 .6 .4],'MenuBar','none',...
'Visible','off');
bc = [.4 .6 .4];
uicontrol('Style','text','Position',[50 80 80 30],...
'String','X','BackgroundColor',bc,'ForegroundColor','w');
uicontrol('Style','text','Position',[50 40 80 30],...
'String','Y','BackgroundColor',bc,'ForegroundColor','w');
uicontrol('style','edit','Position', [120 80 80 30],...
'BackgroundColor',bc,'ForegroundColor','w','Callback',{#My_Callback});
uicontrol('style','edit','Position', [120 40 80 30],...
'BackgroundColor',bc,'ForegroundColor','w','Callback',{#My_Callback});
uicontrol('Style', 'pushbutton', 'String', 'Ok',...
'Position', [100 5 60 30],'Callback', 'close');
movegui(f,'center')
set(f,'Visible','on')
function My_Callback(hObject,eventdata)
p = str2double(get(hObject,'string'));
end
end
Now My_Callback will be called twice but only the last one will be stored in p.
But I want them to be stored like p.x and p.y.
I think I should use Tag, it says:
Tag
string (GUIDE sets this property)
User-specified object label. The Tag property provides a means to identify graphics objects with a user-specified label. This is particularly useful when constructing interactive graphics programs that would otherwise need to define object handles as global variables or pass them as arguments between callback routines. You can define Tag as any string.
But I don't know how (I have about 16 editable boxes),
Thanks for any help.
If I understood your question correctly, you could use the UserData property of uicontrols to store the content of the edit boxes as you enter it, that way it would be very easy to recover them anywhere within your GUI or elsewhere.
Moreover, to facilitate things you can assign names to uicontrols during their creation, so when you need to get one/many of their property you can call the handles with its name instead of the hObject argument.
For example, let's say you name the box with the X data x1, then you can create and store it in the handles structure of the GUI like so:
handles.x1 = uicontrol(...)
so when you need to fetch a property, you can use
get(handles.x1,'Some Property');
So to come back to your question, you could use this syntax to set the UserData property of all the boxes inside My_Callback. Afterward you can recover them in any callback you want. Of course a simple way would be to get the String property of the edit boxes instead of their UserData, but with the latter you can store anything you want which might come handy.
In the following GUI I modified yours to change the callback for the 'OK' button for a function called DisplayData which gets the UserData from each edit box and displays it.
function p = myfun()
clear
clc
f = figure;
set(f,'Position',[200 350 250 150],'Color',[.4 .6 .4],'MenuBar','none',...
'Visible','off');
bc = [.4 .6 .4];
%//===============
uicontrol('Style','text','Position',[50 80 80 30],...
'String','X','BackgroundColor',bc,'ForegroundColor','w');
uicontrol('Style','text','Position',[50 40 80 30],...
'String','Y','BackgroundColor',bc,'ForegroundColor','w');
%//===============
handles.x1 = uicontrol('style','edit's,'Position', [120 80 80 30],...
'BackgroundColor',bc,'ForegroundColor','w','Callback',{#My_Callback});
handles.y1 = uicontrol('style','edit','Position', [120 40 80 30],...
'BackgroundColor',bc,'ForegroundColor','w','Callback',{#My_Callback});
%//===============
uicontrol('Style', 'pushbutton', 'String', 'Ok',...
'Position', [100 5 60 30],'Callback', #(s,e) DisplayData);
movegui(f,'center')
set(f,'Visible','on')
guidata(f,handles); %// Updata guidata
function My_Callback(hObject,eventdata)
p = str2double(get(hObject,'string'));
%// Assign the content of the box to its "UserData" property
set(hObject,'UserData',p)
guidata(f,handles)
end
function DisplayData(~,~)
%// You could do it for all the boxes in your GUI.
x1Data = get(handles.x1,'UserData');
y1Data = get(handles.y1,'UserData');
fprintf('The number in box 1 is %0.2f and the number in box 2 is %0.2f\n',x1Data,y1Data);
guidata(f,handles)
end
end
Sample output:
And after pressing the "OK" button, this string is displayed in the Command Window:
The number in box 1 is 2.00 and the number in box 2 is 4.00
Hope that helps!
As you mentioned in comments, you want to read your values from editbox after the "Ok" button pressed. In order to do this, you should assign your callback function to the "ok" button, not edit boxes:
uicontrol('Style', 'pushbutton', 'String', 'Ok',...
'Position', [100 5 60 30],'Callback', {#My_Callback});
In your Callback function you should read values from all your edit boxes.
I suggest you to use handles when you defined your edit boxes (and better to use it for all the objects):
e1=uicontrol('style','edit','Position', [120 80 80 30],...
'BackgroundColor',bc,'ForegroundColor','w');
e2=uicontrol('style','edit','Position', [120 40 80 30],...
'BackgroundColor',bc,'ForegroundColor','w');
Now, in order to read values, you just use handles of the corresponding edit box.
function My_Callback(hObject,eventdata)
p1 = str2double(get(e1,'string'));
p2 = str2double(get(e2,'string'));
end
Note: this handles (e1,e2) will have cyan color in your code, that means that they are "global" variables, i.e. are used in main code and callback function.
If you don't want to use global handles, you can pass them into callback function. So your callback function will have more arguments.
uicontrol('Style', 'pushbutton', 'String', 'Ok',...
'Position', [100 5 60 30],'Callback', {#My_Callback(e1,e2)});
function My_Callback(hObject,eventdata,e1,e2)
p1 = str2double(get(e1,'string'));
p2 = str2double(get(e2,'string'));
end

Creating 0-100 percent object in Matlab GUI

I am currently creating a GUI(Graphical User Interface) for my Matlab programs and in one of the programs, one of the arguments is a percentage from a whole.
What I want to do is, create some form of object that presents numbers from 0-100 and the user increases the number by 1 or decrease by -1 with every press of the arrow buttons.
Is there such an object that will help me do that? And how do I create it?
Is this what you are looking for:
function test_perc
figh = figure();
% create a text control that will display the 0 - 100 text
texth = uicontrol('Style', 'text', 'Units', 'normalized', ...
'Position', [0.1, 0.1, 0.8, 0.8], 'FontSize', 56, ...
'String', '0');
% set a function that handles key presses (uses handle to the text object)
set(figh, 'WindowKeyPressFcn', #(hobj, ev) percfun(ev, texth));
function percfun(ev, texth)
% check if leftarrow or rightarrow has been
% pressed and modify text accordingly
if strcmp(ev.Key, 'leftarrow')
val = max(0, str2num(get(texth, 'String')) - 1);
set(texth, 'String', num2str(val));
elseif strcmp(ev.Key, 'rightarrow')
val = min(100, str2num(get(texth, 'String')) + 1);
set(texth, 'String', num2str(val));
end
The code above creates a figure and a uicontrol of style 'text'. The figure is then set to respond to keypresses (WindowKeyPressFcn) with a specific function (percfun) that responds to arrows by setting the text of the uicontrol.
If you have any questions - ask, I will elucidate.

how can I get the selected value of radio button?

I am not matlab programmer but I need to create an interface using matlab!
This qusetion should be very easy for matlab programmers :)
I have an interface which contains radio button group panel "OperationPanel"
,4 radioButtons inside it which names are "addBtn, subBtn, divBtn, mulBtn" and I have command button, I want when I click over the button to get the value of the selected radioButton
What is the commad I should use ? I google it and found that if I make
get(handles.NewValue,'Tag');
I tired it but it doesn't work!! Can I hava some help!
Here's a quick example to illustrate how to get the value of a radio-button group component:
function simpleGUI
hFig = figure('Visible','off', 'Menu','none', 'Name','Calculator', 'Resize','off', 'Position',[100 100 350 200]);
movegui(hFig,'center') %# Move the GUI to the center of the screen
hBtnGrp = uibuttongroup('Position',[0 0 0.3 1], 'Units','Normalized');
uicontrol('Style','Radio', 'Parent',hBtnGrp, 'HandleVisibility','off', 'Position',[15 150 70 30], 'String','Add', 'Tag','+')
uicontrol('Style','Radio', 'Parent',hBtnGrp, 'HandleVisibility','off', 'Position',[15 120 70 30], 'String','Subtract', 'Tag','-')
uicontrol('Style','Radio', 'Parent',hBtnGrp, 'HandleVisibility','off', 'Position',[15 90 70 30], 'String','Multiply', 'Tag','*')
uicontrol('Style','Radio', 'Parent',hBtnGrp, 'HandleVisibility','off', 'Position',[15 60 70 30], 'String','Divide', 'Tag','/')
uicontrol('Style','pushbutton', 'String','Compute', 'Position',[200 50 60 25], 'Callback',{#button_callback})
hEdit1 = uicontrol('Style','edit', 'Position',[150 150 60 20], 'String','10');
hEdit2 = uicontrol('Style','edit', 'Position',[250 150 60 20], 'String','20');
hEdit3 = uicontrol('Style','edit', 'Position',[200 80 60 20], 'String','');
set(hFig, 'Visible','on') %# Make the GUI visible
%# callback function
function button_callback(src,ev)
v1 = str2double(get(hEdit1, 'String'));
v2 = str2double(get(hEdit2, 'String'));
switch get(get(hBtnGrp,'SelectedObject'),'Tag')
case '+', res = v1 + v2;
case '-', res = v1 - v2;
case '*', res = v1 * v2;
case '/', res = v1 / v2;
otherwise, res = '';
end
set(hEdit3, 'String',res)
end
end
Obviously you could add more validations on the input numbers and so on...
Have you set handles to the hOjbect? Also don't forget to update the handle after processing the radio button event. Have you looked at this Matlab GUI Tutorial? Scroll down to Part 3 step 5 to see the following example code for three radio buttons:
function fontSelect_buttongroup_SelectionChangeFcn(hObject, eventdata)
%retrieve GUI data, i.e. the handles structure
handles = guidata(hObject);
switch get(eventdata.NewValue,'Tag') % Get Tag of selected object
case 'fontsize08_radiobutton'
%execute this code when fontsize08_radiobutton is selected
set(handles.display_staticText,'FontSize',8);
case 'fontsize12_radiobutton'
%execute this code when fontsize12_radiobutton is selected
set(handles.display_staticText,'FontSize',12);
case 'fontsize16_radiobutton'
%execute this code when fontsize16_radiobutton is selected
set(handles.display_staticText,'FontSize',16);
otherwise
% Code for when there is no match.
end
%updates the handles structure
guidata(hObject, handles);
If you use this syntax below you will get an error:
get(handles.NewValue,'Tag')
What you should be using is
get(eventdata.NewValue, 'Tag')
The point is the when you are looking at the SelectionChangeFcn - what you are essentially looking for is what is the new event that has fired and what is the new value associated with that event. You don't have to use 'Tag' - you can even use 'String' or other properties that may be appropriate in your context.
the code above works on may project ..
function pushbutton_startProcess_Callback(hObject, eventdata, handles)
set(handles.edit1,'String',get(handles.edit2,'String'));
switch get(get(handles.uipanel3,'SelectedObject'),'Tag')
case 'wavelet_method', set(handles.edit1,'String','wavelet_method');
case 'glcm_method', set(handles.edit1,'String','glcm_method');
case 'ewd_method', set(handles.edit1,'String','ewd_method');
case 'wavelet_gclm_method', set(handles.edit1,'String','wavelet_glcm_method');
otherwise, set(handles.edit1,'String','boş');
end