Home > Archive > Matlab > August 2005 > Displaying results in GUI
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Displaying results in GUI
|
|
| Nevine Jacob 2005-08-29, 7:04 pm |
| I have an M-file (implemented for GUI) which produces a certain
result (numerical result) when run. I would like to display this
result on the GUI. How do I do it?
I created a "edit text" component and tried displaying the result to
this component. However, the result is not displayed automatically. I
need to delete the 'default string' of the edit text box in order to
see the result. I am fairly new to Matlab GUI. I would be glad if
someone could help me out.
****************************
Here's the section of code:
function textbox_Callback(hObject, eventdata, handles)
% hObject handle to text17 (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 text17 as text
% str2double(get(hObject,'String')) returns contents of text17
as a double
set(hObject,'BackgroundColor','white');
set(hObject,'String',value);
********************
Thanks,
Nevine
| |
| Jeremy Smith 2005-08-29, 7:04 pm |
| If I'm reading the code right you just need to replace value with
your number, properly converted to a string of course.
set(hObject,'String',num2str(YourNumber)
)
Nevine Jacob wrote:
>
>
> I have an M-file (implemented for GUI) which produces a certain
> result (numerical result) when run. I would like to display this
> result on the GUI. How do I do it?
>
> I created a "edit text" component and tried displaying the result
> to
> this component. However, the result is not displayed automatically.
> I
> need to delete the 'default string' of the edit text box in order
> to
> see the result. I am fairly new to Matlab GUI. I would be glad if
> someone could help me out.
>
> ****************************
>
> Here's the section of code:
>
> function textbox_Callback(hObject, eventdata, handles)
> % hObject handle to text17 (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 text17 as text
> % str2double(get(hObject,'String')) returns contents of
> text17
> as a double
>
> set(hObject,'BackgroundColor','white');
> set(hObject,'String',value);
>
> ********************
>
> Thanks,
> Nevine
| |
| Mark Wiley 2005-08-29, 7:04 pm |
| Ok so my understanding is that you have a function that you've
written that returns a numerical result which you want to display in
a gui. At some point you need to signal to your gui to do the
computation and display the result. There are a few ways you can do
this. I'll describe two which I use most often. One way is in the
gui's opening function ( called guiName_OpeningFcn(hObject,
eventdata, handles, varargin) ). Here you can call your function and
display the result as soon as the gui loads. Or, you could use a
button in the gui which tells the gui to do your process and display
the result.
Let's say we've set up the gui as follows:
We have a textbox whose tag property is: textbox
We have a pushbutton whose tag property is: btnGo
We have named the function you wish to call: foo()
Now you would add this to the body of the opening function should you
choose to execute the command and display the results as soon as the
gui loads:
set(handles.textbox, 'String', foo());
or if you need to use foo's result later on:
result = foo();
set(handles.textbox, 'String', result);
If you choose to do the computation and display upon the pushing of a
button, you would need this code to appear:
function btnGo_Callback(hObject, eventdata, handles)
result = foo();
set(handles.textbox, 'String', result);
I hope this helps.
Regards
Mark
Nevine Jacob wrote:
>
>
> I have an M-file (implemented for GUI) which produces a certain
> result (numerical result) when run. I would like to display this
> result on the GUI. How do I do it?
>
> I created a "edit text" component and tried displaying the result
> to
> this component. However, the result is not displayed automatically.
> I
> need to delete the 'default string' of the edit text box in order
> to
> see the result. I am fairly new to Matlab GUI. I would be glad if
> someone could help me out.
>
> ****************************
>
> Here's the section of code:
>
> function textbox_Callback(hObject, eventdata, handles)
> % hObject handle to text17 (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 text17 as text
> % str2double(get(hObject,'String')) returns contents of
> text17
> as a double
>
> set(hObject,'BackgroundColor','white');
> set(hObject,'String',value);
>
> ********************
>
> Thanks,
> Nevine
| |
| Nevine Jacob 2005-08-29, 7:04 pm |
| The problem I am facing with is not with the numerical result. The
problem is that the results are not displayed automatically when the
GUI is run. I need to modify/delete the 'default text' in the 'edit
textbox' in order to see the result.
- Nevine
Jeremy Smith wrote:[color=darkred]
>
>
> If I'm reading the code right you just need to replace value with
> your number, properly converted to a string of course.
>
> set(hObject,'String',num2str(YourNumber)
)
>
> Nevine Jacob wrote:
this[color=darkred]
result[color=darkred]
> automatically.
order[color=darkred]
if[color=darkred]
> MATLAB
text[color=darkred]
| |
| Jeremy Smith 2005-08-29, 7:04 pm |
| Then the question becomes how are you having the GUI acquire the
data?
I would recommend getappdata/setappdata.
setappdata(0,'DataName',Data)
In the GUI get the data.
Data = getappdata(0,'DataName');
Then you should be able to display it. If your data is going to
continually change you'll have to keep calling a function to update
it.
| |
| Mark Wiley 2005-08-29, 7:04 pm |
| Nevine Jacob wrote:
>
>
> The problem I am facing with is not with the numerical result. The
> problem is that the results are not displayed automatically when
> the
> GUI is run. I need to modify/delete the 'default text' in the 'edit
> textbox' in order to see the result.
>
If the tag property for your textbox is: textbox
the code: set(handles.textbox, 'String', 'your new text');
will overwrite the default text of the textbox. You can
display the result of your function upon initialization of
the GUI by putting this line in the opening function of
your gui with the result of your function in place of
the: 'your new text'.
<Put in opening function>
result = yourFunction(args);
set(handles.textbox, 'String', result);
I'm able to display the numerical result with out converting it to a
string.
Hope this helps
mark
| |
| Nevine Jacob 2005-08-29, 7:04 pm |
| 1) Are you sure that by using the code:{ set(handles.textbox,
'String', 'your new text') } would result in displaying the new text
instead of the earlier text?
The reason for my question is because this is what I do by using the
code: { set(hObject,'String',value); }
2) Also I would not be able to place the code in the Opening
function, the reason being, I need to run the entire code in order to
get the final result.
3) Also you had mentioned:
{ If the tag property for your textbox is: textbox
the code: set(handles.textbox, 'String', 'your new text'); }
However, when I do this, I get the error "Invalid figure property:
'String'". It however works if I use "hObject" instead of
"handles.textbox". Any comments?
Thanks,
Nevine
Mark Wiley wrote:
>
>
> Nevine Jacob wrote:
> The
when[color=darkred]
> 'edit
>
> If the tag property for your textbox is: textbox
>
> the code: set(handles.textbox, 'String', 'your new text');
>
> will overwrite the default text of the textbox. You can
> display the result of your function upon initialization of
> the GUI by putting this line in the opening function of
> your gui with the result of your function in place of
> the: 'your new text'.
>
> <Put in opening function>
> result = yourFunction(args);
> set(handles.textbox, 'String', result);
>
> I'm able to display the numerical result with out converting it to
> a
> string.
>
> Hope this helps
>
> mark
| |
| Mark Wiley 2005-08-29, 7:04 pm |
| I'm no expert but let me explain things as I see them and as has
worked for me in my experience...
> 1) Are you sure that by using the code:{ set(handles.textbox,
> 'String', 'your new text') } would result in displaying the new
> text
> instead of the earlier text?
>
> The reason for my question is because this is what I do by using
> the
> code: { set(hObject,'String',value); }
Assuming the tag property of your Edit Text box is: textbox
then yes I am sure that the line:
set(handles.textbox, 'String', 'new text');
will over write any existing text in that text box. You can use the
hObject method, but be careful because you have to know exactly what
hObject is referring to. Your original code < set(hObject,
'String', value) > will work fine in the callback function for
the Edit Text box. However, put that same line in the callback
function for any other GUI element and it will update the String
property for that element, not our original Edit Text box. (Sorry if
you already know all this). I personally like using the
handles.<tagName> convention because it is always clear exactly
which GUI element I am referring to.
> 2) Also I would not be able to place the code in the Opening
> function, the reason being, I need to run the entire code in order
> to
> get the final result.
If by 'run the entire code' you mean execute an m-file external to
the GUI, you can call that function in the opening function. If,
however, you need to do other things in the gui before displaying
this result, you would need to add the set(...) function to some
other call back with in the GUI (like a button or text box).
> 3) Also you had mentioned:
> { If the tag property for your textbox is: textbox
> the code: set(handles.textbox, 'String', 'your new text'); }
>
> However, when I do this, I get the error "Invalid figure property:
> 'String'". It however works if I use "hObject" instead of
> "handles.textbox". Any comments?
In the code of your first post the comments for textbox_Callback(...)
say:
%Hints: get(hObject,'String') returns contents of text17 as text
This indicates to me that the tag property of your textbox is: text17
not: textbox
I'd double check the tag property of your textbox. I'm guessing if
you do NOT change the tag and issue the command: set(handles.text17,
'String', 'new text'); that the text in the text box will be
updated, assuming no changes have been made to the tag property since
your first post.
hope this clarifies. I'll check the post a couple more times today
if not.
Mark
| |
| Nevine Jacob 2005-08-31, 7:01 pm |
| Hi Mark,
Sorry for responding late. I tried what you said on my code but it
did not seem to work. I realized it was some problem in my GUI code.
Now I've rewritten my GUI code from scratch and now it seems to work
fine.
Thanks so much for the help.
- Nevine
Mark Wiley wrote:
>
>
> I'm no expert but let me explain things as I see them and as has
> worked for me in my experience...
>
>
using[color=darkred]
>
> Assuming the tag property of your Edit Text box is: textbox
> then yes I am sure that the line:
> set(handles.textbox, 'String', 'new text');
> will over write any existing text in that text box. You can use
> the
> hObject method, but be careful because you have to know exactly
> what
> hObject is referring to. Your original code < set(hObject,
> 'String', value) > will work fine in the callback function for
> the Edit Text box. However, put that same line in the callback
> function for any other GUI element and it will update the String
> property for that element, not our original Edit Text box. (Sorry
> if
> you already know all this). I personally like using the
> handles.<tagName> convention because it is always clear
> exactly
> which GUI element I am referring to.
>
> order
>
> If by 'run the entire code' you mean execute an m-file external to
> the GUI, you can call that function in the opening function. If,
> however, you need to do other things in the gui before displaying
> this result, you would need to add the set(...) function to some
> other call back with in the GUI (like a button or text box).
>
>
> property:
>
> In the code of your first post the comments for
> textbox_Callback(...)
> say:
> %Hints: get(hObject,'String') returns contents of text17 as text
>
> This indicates to me that the tag property of your textbox is:
> text17
> not: textbox
>
> I'd double check the tag property of your textbox. I'm guessing if
> you do NOT change the tag and issue the command:
> set(handles.text17,
> 'String', 'new text'); that the text in the text box will be
> updated, assuming no changes have been made to the tag property
> since
> your first post.
>
> hope this clarifies. I'll check the post a couple more times today
> if not.
>
> Mark
| |
|
| No problem. Glad it's working.
Mark
|
|
|
|
|