I am making a GUI in which there is a multi-line edit box.
the user will have to input the 3 x-y coordinates in this edit box at a time:
[345.567 123.123]
[390.567 178.098]
[378.000 125.987]
by clicking the push button I want these coordinates to be "saved" in the Matlab GUI workspace in the form of a matrix and by clicking another push button "reloaded" from the workspace so that they can be available for future use.
How can I do that?
Can anyone guide me with this? Help would be appreciated!
There are a number of ways to manage data in GUIDE-generated GUIs. The easiest IMO is to use guidata.
For example inside the "Save" push button callback, you would access the edit box string contents, parse as matrix of numbers as save it inside the handles structure.
function pushbuttonSave_Callback(hObject, eventdata, handles)
handles.M = str2num(get(handles.edit1, 'String'));
guidata(hObject, handles);
end
Next in the "load" button, we do the opposite, by loading the matrix from the handles structure, converting it to string, and set the edit box content:
function pushbuttonLoad_Callback(hObject, eventdata, handles)
s = num2str(handles.M, '%.3f %.3f\n');
set(handles.edit1, 'String',s)
end
If you want to export/import the data to/from the "workspace", you can use the ASSIGNIN/EVALIN function:
assignin('base','M',handles.M);
and
handles.M = evalin('base','M');
to save data:
setappdata(h,'name',value)
to load data:
value = getappdata(h,'name')
values = getappdata(h)
where h is the handle you store the data in, name is the variable of the data, value is the actual data.
Related
Here is my code for the callback function.
function Next_Callback(hObject, eventdata, handles)
display('Click Next');
handles.imgLNum = strcat('I1_',num2str(handles.imageNumber),'.png');
handles.imgRNum = strcat('I2_',num2str(handles.imageNumber),'.png');
handles.imageLeft = strcat(handles.directory,handles.imgLNum);
handles.imageRight = strcat(handles.directory,handles.imgRNum);
axes(handles.img1);
imshow(handles.imageLeft);
axes(handles.img2);
imshow(handles.imageRight);
handles.imageNumber = handles.imageNumber+1;
I have a button called "Next" on my GUI and I want to load the next image into the axes when it is clicked. handles.imageLeft and handles.imageRight have the path for the images. When the hit the button the first time, the axes gets updated with the images. But subsequent clicks on the button do not update the axes. But the 'Click Next' text is displayed in console, so I know the callback function is being called.
Thank you #Adiel for the help. I find the problem following your suggestions. I updated the handles.imageLeft and handles.imageRight and I added the code guidata(hObject, handles); I think this function updates the handles (This is my first MATLAB GUI, so I am not very sure). Now the problem is fixed.
I am asked by my professor to add a GUI for my Matlab code. My program receives an image as an input and returns a string.
The GUI should enable me to browse the image and then display it. Then I need to use that image in the Matlab code.
To browse and display the image, I created a pushbutton control and wrote the following in its callback
[baseFileName, folder] = uigetfile('*.jpg');
fullFileName = [folder baseFileName];
rgbImage = imread(fullFileName,'jpg');
imshow(rgbImage);
I added a second pushbutton and the Matlab code (which has a file name main.m) inside its callback. This function needs the image displayed above as an input, and its output (which is a string) needs to be displayed in the GUI.
I am facing a few problems:
I want the image to be displayed in a specific position.
How can I call the function in the push button?
How can I access and use the image in the first push button to the second push button?
Some hints on how you can get started with your problems:
You could create an axes object in your figure, whose position can be defined. then just plot the image on that axes. Do all that in the callback
Calling a function from a callback should not be a problem
Save the image in structure, then you can use for example setappdata and getappdata to pass it between callbacks, i.e. when your figure handle is h.fig and your structure called d:
setappdata(h.fig,'d',d)
in the first callback, and to retrieve it, in the second:
d = getappdata(h.fig,'d');
I've written a Matlab program which counts different values when are particular button is pressed (so far just the numbers of "yes" and "no"). I'm trying to add a key listener so that when I press, for example, n on the keyboard the button is pressed, or the same actions are completed. I have tried the addListener and keyfunclistener functions but neither seems to be working.
Here is an example of the button:
no=uicontrol(h_fig,'callback',countnonerve,'position',[.65 .07 .1 .08],'string','No','style','pushbutton','Units','normalized');
Any suggestions? It would be very helpful I'm not familiar with MatLab
You could try using the KeyPressFcn property of the figure to record/capture individual key presses:
function keypress_Test
h = figure;
set(h,'KeyPressFcn',#keyPressCb);
function keyPressCb(src,evnt)
disp(['key pressed: ' evnt.Key]);
end
end
The above code can be pasted into a file and saved as keypress_Test.m. A figure is created and a callback assigned to the KeyPressFcn property.
It can be easily adapted for your GUI. Wherever you have access to the figure handles (probably the h_fig from above) just add set(h_fig,'KeyPressFcn',#keyPressCb); and paste the keyPressCb code into the same file.
If you are counting the number of times a certain key is pressed, then you will have to save this information somewhere..probably to the handles structure. Is that the case? If so, then you can easily access this from the callback
function keyPressCb(src,evnt)
disp(['key pressed: ' evnt.Key]);
% get the handles structure
handles = guidata(src);
if ~isempty(handles)
% do something here - increment count for evnt.Key
% save the updated count
guidata(src,handles);
end
end
Try it out and see what happens!
I am trying to learn GUI programming in matlab and for that purpose i am trying to create a simple multiplication calculator. I have done some programms in matlab without GUI but i am having difficulty in understanding GUI programming in Matlab. I have created the GUI but i dont know how to do the programming for that.
This is my GUI i made
EDIT TEXT 1; string= 0
EDIT TEXT 1; tag= edit1
EDIT TEXT 2; string= 0
EDIT TEXT 2; tag= edit2
STATIC TEXT 1; string= X
STATIC TEXT 1; tag= text3
STATIC TEXT 2; string= 0 (for showing results)
STATIC TEXT 2; tag= result
PUSHBUTTON; String= Calculate
PUSHBUTTON; tag=push_calc
i saved the given GUI in the name of "add" and created add.m . Can you tell me how to do programming for given gui.
The basic idea of matlab gui programming is the following:
Set up the figures
Enter the message loop
Both steps are taken care of by using the matlab gui editor (guide). The important thing is that you give control of the program flow over to the message loop. In order to get things done, you can tell the message loop to call a function whenever something happens.
In the gui editor, right click your pushbutton and select "View Callbacks -> Callback". This will automatically create such a function in you .m file where you can specify what happens when you push the button.
For a better understanding take a look at the Callback property of the pushbutton. Guide will have entered something like add('push_calc_Callback',hObject,eventdata,guidata(hObject)) which calls the main function (add) as a wrapper for your new callback function. You could have done that by yourself in the property editor or programmatically in the startup code.
I guess you want the following to happen:
Get the string values of edit1 and edit2
Convert the strings to numeric values
Perform the calculation
Set the string value of text3 to the string representation of the result
You can access the properties of the gui elements by using the handles available to you as the third function argument and the get and set functions. The handles structure is created by guide and the elements are named the same as the tag you specified.
In matlab code, this could look like this:
% --- Executes on button press in push_calc.
function push_calc_Callback(hObject, eventdata, handles)
% hObject handle to push_calc (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
x_string = get( handles.edit1, 'String');
y_string = get( handles.edit2, 'String');
x_numeric = str2num( x_string );
y_numeric = str2num( y_string );
result_numeric = x_numeric * y_numeric;
result_string = num2str( result_numeric );
set( handles.result, 'String', result_string);
Edit: The question is what is handles.edit1 and so on.
Whenever you want to do something with a widget like a button or a textbox, you have to be able to tell matlab exactly what widget you mean. Guide does a few things behind the scenes. One of them is to call uicontrol, which creates the widget and returns a random but unique number. This is a bit like a bank account number in the way that it is a handle to a resource that can be used to manipulate it. When you create a new pushbutton in guide and assign the tag "clickme" in the property editor, guide creates the pushbutton and stores the handle in a structure handles.clickme. That gives you an easy way to get the handle of any widget that you created if you can still remember what tag you assigned it.
Let's take the first line of the function:
x_string = get( handles.edit1, 'String');
That calls the function get with some number you should not care about as long as it the same number that matlab associates with the edit1 widget and a property name from the property editor, in this case 'String'. That would be the same as you clicking through all the window elements until the property editor shows a tag of 'edit1', and for that object you find the value for the property named 'String'.
The properties get updated automatically whenever you type in new text, move a slider, change the window size and so on. It works the other way around, too. If you modify the 'Position' property with set( handles.edit1, 'Position', [20 20 100 30]), then the widget is automatically moved and re-sized to the specified position.
I have a checkbox on GUI that draws a rectangle on a live video feed, however, I need the rectangle to dissapear or be deleted when I uncheck it.
does anyone have any idea how to do this?
This is my code, I have tried putting things in else, but nothing works.
function Box(hObject,eventdata)
if (((get(hObject,'Value') == get(hObject,'Max'))))
% Checkbox is checked-take appropriate action
hold on;
rectangle('Position',[50,50,100,100],'EdgeColor','r')
else
end
You need to save the handle created by the function rectangle. Then add this handle to the big handle of your GUI so that you are able to have access to it once the callback is called again.
So modify your function like so
function Box(hObject,eventdata,handles)
if (((get(hObject,'Value') == get(hObject,'Max'))))
% Checkbox is checked-take appropriate action
hold on;
handles.rectangleSave=rectangle('Position',[50,50,100,100],'EdgeColor','r');
guidata(handles.output,handles);
else
delete(handles.rectangleSave);
end
If you have never used handles, please have a look here :
http://www.matlabtips.com/on-handles-and-the-door-they-open/
handles.output usually stores the handle to the big interface window as explained here :
http://www.matlabtips.com/guide-me-in-the-guide/