Program value into edit text box GUI - matlab

I am making a GUI in Matlab and am trying to incorporate two different codes into the same GUI to analyze a set of data taken from an Excel file. How do I display the result from my first code, being just a single value, into an edit text box. I've tried messing with a string but have not had much luck. Any suggestions?

There are several ways to do it, all of them depending on the structure of the GUI yuo've built.
One possibility could be:
you have a pushbutton: when the user pushes it, the pushbutton callback calls your first script which stores the result in a variable, say out_var (you should have inserted in the callback either directly the code or the call to a function / ".m" file)
Your GUI has a static text uicontrol in which you want to write the value of "out_var" (it is better using "static text" uicontrol rather than "edit text" uicontrol just to show a string being the first "not editable").
To display the value of "out_var" in the "static text" uicontrol, you can insert the following code in the pushbutton callback:
set(handles.text1,'string',num2str(out_var));
where "text1" is the handle of the static text uicontrol.
You can "find" its name inspecting the static text uicontrol properties with the GUIDE tool looking for the "Tag" property.
Hope this helps.

Related

MATLAB uitable created by GUIDE to update data sets

I'm currently working on a GUI that has a uitable within it. The idea is to allow the user to input a string and press a button to add a new row of cells, like so: ui_table_currently.
In addition to this, when the user clicks 'accept', the data set so far would be saved. This means if the user were to load up the GUI again, the same data set would be shown as when they closed down the GUI.
In practice, I've managed to save the data set with the users input into the workspace in MATLAB, however, I cannot update the data set that is shown in the uitable when I reload the GUI, unless I use GUIDE and change uitable via (Inspector > Table Property Editor > Data > Change data value to the selected workspace variable).
My question is this: How do I get uitable to retain the data set it has when I close it, without manually having to change it through GUIDE?
You should have two files created with GUIDE. For the sake of example, let's say your's are named raimo.fig and raimo.m. Let's also say you gave your uitable the tag, 'table_1'.
If you edit the raimo.m file, you should see a line like this
% --- Executes just before analysis is made visible.
function raimo_OpeningFcn(hObject, eventdata, handles, varargin)`
Load your data from the file within this function, and then give it to your user data.
Here's a made up example of how to do it:
a = load('yourAmbiguousData');
set(handles.table_1,'table',a);
I don't know how your data is saved, or what is called, but if you have pulled it out of your uitable correctly, you can just put it back in there with a set command.

Selection Callback for text input

I'm using MATLAB GUIDE to build a GUI to control a piece of hardware. I have a data table the user needs to fill, and every time they select a cell a new configuration is sent to the hardware. This is easily done using the CellSelectionCallback().
However, I also have a single reference value that is currently being entered into an edit textbox. I would like for the GUI to send a new configuration to the hardware whenever a user selects the textbox, but before they enter their new value. Is there a similar SelectionCallback function that can be used for the textbox? The best solution I've come up with is to create a 1x1 table instead of an edit box.
Your best bet is going to be to use a Java control since the standard MATLAB uicontrol does not have a focus callback. You can use a JTextField object, add it to your GUI with javacomponent and then register the FocusGainedCallback.
jtext = javax.swing.JTextField();
jtext.setHorizontalAlignment(javax.swing.JTextField.CENTER);
% Add the control to your GUI
htext = javacomponent(jtext, [10 10 200 10]);
% Register a callback to be executed when the box is selected
set(htext, 'FocusGainedCallback', #yourCallback)

Retrive data from gui object while executing a script file

I have a simple gui where the user enters some data and executes a script file afterwards.
I want to extract the data entered in the text boxes and tables from the gui.
How can I perform that ?
Note: the gui is just used to enter data. That is the user has to call the script file from the workspace after entering the data in the gui.
To extract data from a uicontrol you can use wwhat is called "dot notation":
h=uicontrol('styile','edit');
text=h.string;
(more uicontrol parameters should be defined)
As alternative, you can use the "old style" function get
h=uicontrol('styile','edit');
text=get(h,'string')
About the user required to manually run the script, you can also "automate" it by assigning the script to the callback property of a uicontrol (e. g. to a pushbutton)
h=uicontrol('style','pushbutton','callback','my_script')
the my_scriptm-file will be execute when the user presses the pushbutton.
Hope this helps.
Right-click the uicontrol in the gui and select View Callbacks->Callback.
If your uicontrol is an editbox these lines would set the variable myData in the base workspace to the data entered in the editbox if you add these lines to the callback function:
assignin('base','myData',get(hObject,'String'));
If your uicontrol is something else than an editbox the 'String' might be 'Value'. Right click the uicontrol and select the Property Inspector to find the attribute of interest.

how to hide existing uicontrol in Matlab before printing figure?

I am using a Matlab based program that does some nice plots of some model results. It adds uicontrol slides and buttons in figures. I have no expierence with gui programming in Matlab, and I dont need it, I just wanna add on my matlab script a couple of lines to hide slides and buttons. I can do it manually from the property editor and set "Visible" to "off", but I was reading the Matlab manual and it does not explain how to retrieve an existing uicontrol and change its properties. Any hint? I tried this with no luck:
b = get(gcf,'uicontrol');
set(b,'Style','pushbutton','Visible','off');
Thanks
You simply need to access the element from the handles structure and change its property from there.
For example, if the pushbutton is stored in the handles structure like this:
handles.b %// Whatever name you gave it and see in the Property Inspector
you can make it not visible using the command
set(handles.b,'Visible','off')
and likewise for every other properties.
Little trick: If you need to repeatedly turn on and off elements of your GUI, you can put them in an array of handles for example in the Opening_Fcn of the GUI and change them all at once using this array anywhere in the GUI. This way you won't have to always call them one by one which can be tedious.
Example:
handles.AllButtons = [handles.button1; handles.button2; handles.button3]
this contains the handles to 3 pushbuttons let's say. Now if yu need to turn them all off/on at the same time, you can do:
set(handles.AllButtons,'Visible','off')
instead of doing
set(handles.button1,'Visible','off')
set(handles.button2,'Visible','off')
set(handles.button3,'Visible','off')
From GUIDE, you can check the actual name of any uicontrol component in the Property Inspector. Here is a screenshot from a GUI I made with GUIDE:
In this case, the Tag associated with the button is pushbutton28_ReferenceChannelApply.
Therefore, in order to change any of its properties I would need to use:
set(handles.pushbutton28_ReferenceChannelApply,'Property','value')
EDIT 2
You can look for pushbuttons in your GUI with the findobj command like so:
FindButtons = findobj('Style','push')
which will output an array of handles to those pushbuttons. Then you can query their properties using the get command:
get(FindButtons(1))

Matlab GUI: Dynamically changing the popup menu

I have a GUI that I want to add a popup menu to it.
The popup menu fields that should be shown are saved in the file targets.txt.
When I open my program, I want the popup menu to include the lines from the mentioned file above.
I'm doing this because I want the popup menu to change dynamically in the program. Since it will include directories paths that the user entered in another field, I'm saving the directories paths in a file, and each time a user enters a folder, I set the popup menu according to the file. (I did it and it works fine)
Since function myFunction_OpeningFcn(hObject, eventdata, handles, varargin) is called only after calling the "create function" of each component of the GUI, I couldn't do the initialization in the "opening function" of the program. Instead, I had to to something like this:
function databaseMenu_CreateFcn(hObject, eventdata, handles)
if ispc&&isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
handles.databaseMenuObject=hObject; % (1) see below
guidata(hObject, handles);
(1): I save the popup menu object in handles so that I can use it in the opening function.
And then, in the opening function I can do:
fid_r = fopen('targets.txt', 'r');
C = textscan(fid_r, '%s');
set(handles.databaseMenuObject,'String', C{1});
So, when someone adds a new "database" folder in the program, the popup menu changes (I add the folder that was chosen by the user to the file, and then I set the popup menu to have its field from the file (the function above). So it'll look like that:
I don't like the design of my code, and I couldn't figure out how to do it in a different way, is there a way to make the "create function" of the "popup menu" to be called after the "opening function" of the program? Or is there a better way to achieve my goal?
Background
A few things that may be helpful:
You can define additional functions as needed within a GUI; you are not limited to the callbacks defined.
Tagging figure elements makes them easy to find from any callback.
gcbf returns the current callback figure.
union can return the union of cell arrays of strings.
Suggestions
Here is how I might write such a function. This puts everything in one place, the function can be called from anywhere within your GUI, it automatically updates the cached list, and eliminates any duplicate entries.
You would call this code at the end of your init code, as well as the callback for adding folders. The Tag can be set by right-clicking and setting properties within GUIDE.
updatePopupMenu()
popupMenuHandle = findobj(gcbf,'Tag','myPopupMenuTag');
popupMenuContents = get(popupMenuHandle,'String');
% Initialization
if isempty(popupMenuContests)
fid_r = fopen('targets.txt', 'r');
C = textscan(fid_r, '%s');
popupMenuContents = C{1};
end
% Join
otherFields = howeverYouGetFieldsFromOtherList();
combinedContents = union(popupMenuContents, otherFields);
% Save
set(popupMenuHandle,'String', combinedContents);
fid_w = fopen('targets.txt','w+');
for i = 1:length(combinedContents)
fprintf(fid_w,'%s\n',combinedContents{i});
end
fclose(fid_w);
end
Alright I now understand what you want to achieve, though I am not sure whether I understand the problem I hope this helps:
Judging from the description this seems like a logical order for things to happen:
1: Initalization, just initialize everything, you already know that you will have a dropdown menu but you just don't know the content yet. Therefore just start with a defaultoption or empty (possibly invisible).
2: Update, As soon as the users saves new input, you update the list.