Edit Text as input ... handling?

1 view (last 30 days)
Hello kity
Hello kity on 20 Dec 2012
Hi,
currently i get graphs/ histograms which show a nice division, but sometimes the data varies so i have outliners, then i want to change the axes. i can do that by zooming but what i want is that I choose the Xmax and Xmin, Ymax , put them in an edit text, then get that value, use that value to plot new graph with adjusted axes....
this is an edit text box
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit3 as text
% str2double(get(hObject,'String')) returns contents of edit3 as a double
Min_value = str2double(get(hObject,'string'));
if isnan(Min_value)
errordlg('You must enter a numeric value','Bad Input','modal')
return
end
So i want to use that Min_value in a different function, but how do i get that value to a different function.
I was wondering, does edit text automatically saves what is in the box...
[EDITED, Jan, code formatted]
  1 Comment
Bart Boesman
Bart Boesman on 20 Dec 2012
You should declare a variable handles.Min_value and assign it in your callback. I always make an initialization file in which I initialize a set of variables that are to be used in different callbacks or functions.
Bart

Sign in to comment.

Answers (4)

Jan
Jan on 20 Dec 2012
The contents of the edit text does not have to be stored, because it is stored as context already.
Is the "other function" a callback of the GUI? The sharing of variables between different subfunctions of a GUI has been discussed over a hundred times in this forum, such that a search will be useful.
You can either store the value in the handles struct, see help guidata. Or you can use a unique tag for the edit UICONTROL, such that findobj can find it dynamically. Then str2double(get(hObject,'string')) replies the current contents as number, which can be applied as input for the YLIM command.

Hello kity
Hello kity on 20 Dec 2012
I seached a lot, but somehow I dont get how it works. the entire concept of handles is vage....
'the other function' is another m file (function).
My question is, how do I get that (example) Min_value in another function or callback...

Jan
Jan on 20 Dec 2012
Edited: Jan on 20 Dec 2012
You can store all values you want to in a struct and call it "handles", as GUIDE does it as default. Any other name would be smarter, because too many users are confused by the similarity to the handles of GUI objetcs.
Now the command guidata(H, handles) stores this struct into the figure with the handle H. To increase the usability of the command guidata, the handle of any other child object of the figure works also. In the next step, the struct can be obtained by guidata also in each callback.
function callback1(hObject, EventData)
handles = guidata(hObject); % Or: guidata(ancestor(hObject, 'figure'))
handles.abcdefg = rand(5);
guidata(hObject, handles); % Stor it in the figure again
function anotherCallback(hObject, EventData)
handles = guidata(hObject); % Or: guidata(ancestor(hObject, 'figure'))
disp(handles.abcdefg);
By this way you can store values, which beloing to a figure.
If you call another M-file, you can either provide the figure's handle as input:
function someOtherCallback(hObhect, EventData)
FigH = ancestor(hObject, 'figure');
theExternalFunction(FigH);
...
and obtain the handles struct of the figure exactly in the same way as in the local callbacks using guidata. These methods are explained exhaustively in help guidata and doc guidata, and you can search for "guidata" or "sharing variable" and even "gui" will find some matchs.
  2 Comments
Jan
Jan on 20 Dec 2012
Edited: Jan on 20 Dec 2012
@Walter: The anchor of your link is dead. Unfortunately this wikia page is under construction, such that all old links in this forum point to nowhere. Even "faq6.1" etc got meaningless, because the numbering has been changed.
Therefore I stopped posting links to this formerly very important FAQ page. What a pitty.

Sign in to comment.


Hello kity
Hello kity on 21 Dec 2012
Edited: Hello kity on 21 Dec 2012
I dont understand your explanation. I think what I want is something easier.
function Min_Callback(hObject, eventdata, handles)
% hObject handle to Min (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of Min as text
% str2double(get(hObject,'String')) returns contents of Min as a double
*MinNumber=get(hObject,'Value');*
% --- 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)
*so what do i type here to get MinNumber*
  1 Comment
Jan
Jan on 21 Dec 2012
@Matlab newbie: Please post comments in the comment section, because they are not answers. Currently it is not clear which answers this message is related to.
I have posted the a solution already. As long as you do not explain how you can recognize the handle of the min value edit field, a more explicit answer is not possible. Using guidata() to get the handles struct willmost likely allow to obtain the handle of the edit field. But GUIs created by GUIDE are such messy, that I do not try to speculate, how this could work in your case.
Please note that "I do not understand your explanation" is not helpful to improve the answer. If you spend the time to read my answer again and ask a specific question, I can improve it. But I do not think that the 4 relevant lines of code are too hard to understand.

Sign in to comment.

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!