matlab function handle inside gui - matlab

I created a GUI layout using Matlabs GUID application, essentially my problem is simple, i have a text box where I want to enter data, and a push button which i want to display that value entered inside the text box, the text box is define as
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
myString = get(hObject, 'String')
set(hObject,'Value',str2num(myString));
at this point, i entered some numerical value into the text box, lets say 44, now 44 is stored inside the "Value" element of hObject for this function.
Now I want to output this value when a button is pushed
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)
fHandle = #edit1_Callback
get(fHandle,'Value')
^^^on this last line i should see some output(since i dont have a ";" which allows matlab to print out the data), the issue is....i get an error that says "Conversion to double from function_handle is not possible." , shouldnt I be able to call "get" using a function handle which points to my earlier function, thanks for any help!

I would go for
get(handles.edit1,'value')
All your uicontrols are stored in the handles. Knowing the tag of your uicontrol (for your edit box I guess this is edit1), you can get the handles of this object with handles.edit1.
You can even write
my_value = get(handles.edit1,'Value');

Related

How to save some values from gui into a text file and load them elsewhere as a number?

I have 2 values from an edit box and I want to write them in a txt file when I press a push button
function Masaedit_Callback(hObject, eventdata, handles)
% hObject handle to Masaedit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
function Arias_Callback(hObject, eventdata, handles)
% hObject handle to Arias (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA
m=str2double(get(handles.Masaedit, 'string'));
assignin('base','m',m)
A=str2double(get(handles.Arias, 'string'));
assignin('base','A',A)
twovalues = fopen('twovalues.txt','w');
fprintf(twovalues,'%6d\t%3d',m,A);
fclose(twovalues);
But I want to save them as:
m=value;
A=value;
After the values are saved , I want to load the text file in a function like:
function xypr=twovalues (m,A)
CD=1;
load ('twovalues.txt',m,A)
ad=(-1/2)*((CD*A)/m);
end
But that it seems a little tricky and I don't know how should I do the conversion because for A,m = I need to load them as a string and for the result I should use something like str2double.
I actually made a quick youtube video in response to your question, so if you want to watch that you can(I go into more depth there, but it is meant to be a bit more general):
https://youtu.be/GQtYAT36CZ4
Otherwise, a short answer is that it depends on whether you need that data available outside of matlab or not. Your approach is mixing those two - generally you should either set it up to not be accessible outside of matlab using the save command (save('filename.mat', 'm', 'A') and load('filename.mat', 'm', 'A')) or write to a csv with column headers (use writetable and readtable with a table containing only your variables for example). Note that for the purposes of your answer, csv is a text format. Of course it is possible to use fprintf as you describe, but that is really the hardest way of doing it. I hope that helps!
Edit: I note in your question that part of your issue is related to the string vs double nature of the text box. The method I suggest here would cut in after you convert to double. No method will get rid of that issue, unless you use a gui element which outputs as double.

MATLAB - passing new value of parameter from GUI into .m script

I want to make simple GUI for my script, where I can edit parameter values and running that script.
I've created example scipt and GUI with 2 buttons. I'cant put script code into GUI code, I will need to aply it on much larger script.
So, script code:
number = 10;
variable(1:10) = NaN;
for i = 1:10;
variable(i) = i * number;
end
figure
plot(variable)
Push button code, that is working fine. script is name of .m file, not function:
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)
evalin('base','script')
But I dont know what to type into edit button code If i want to change value of "number" in the script:
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (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 edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
And last thing, sometimes when I try to plot more graphs, one figure overwrites GUI figure and I can see only buttons, but not whole GUI.
Thank you fot any help.
The hint given in the last piece of code is enough.
a = str2double(get(hObject,'String'));
This will save the input value as a double, in the callback function's stack.
In order to pass this value to the caller script's (base) stack, use assignin
assignin('base', 'number', a)

GUI Workflow command

I am developing a GUI in Matlab and I would like to know which is the workflow when you click a button. Being more specific, I would like to know 'what happens' when I click a button, because its callback is not triggered.
If you are using GUIDE for developing, every time you add a button to your GUI a chunk of code is generated:
% --- 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)
This function is called every time you push the said button. So, if you need something to be executed when you click the button you just need to add the lines of code you want to execute below that generated chunk of code. For example, imagine you have an edit text variable called edit1 with value
edit1 = 'hello';
If you want to interact with it you need to call handles, but first you need to create a global variable:
%set the current figure handle to main application data
setappdata(0,'figureHandle',gcf);
%set the handles to figure's application data
setappdata(gcf,'EDIT1',handles.edit1);
Then, in the callback function of your button you need write the following:
% --- 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)
figureHandle = getappdata(0,'figureHandle');
EDIT1 = getappdata(figureHandle,'EDIT1 ');
new_string = 'updated string';
set(EDIT1, 'String', new_string);
Hope this helps

Check box in MATLAB GUI

My GUI has two checkboxes, namely colourcheck and Texturecheck, below a single search button. If I click on the search button, it should check for both types mentioned above and respective program should run, also if both box are in 'MIN' position i.e., not checked it should give a message to user stating select type of search.
I've clipped search_callback program.
function Search_Callback(hObject, eventdata, handles)
% hObject handle to Search (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data
% --- Executes on button press in colourcheck.
function colourcheck_Callback(hObject, eventdata, handles)
% hObject handle to colourcheck (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data
% Hint: get(hObject,'Value') returns toggle state of colourcheck
if (get(hObject,'Value') == get(hObject,'Max'))
Search_Callback(hObject, eventdata, handles)
else
% Checkbox is not checked-take approriate action
end
However I am not able to meet the requirements. Please help me, any solution is appreciable.
From the description in your question, you don't want Search_Callback called when you click colourcheck_Callback. Instead you want some other action taken when the search button is clicked based on which check boxes are selected. You could use a callback like the following for your search button:
% --- Executes on button press in search.
function search_Callback(hObject, eventdata, handles)
% hObject handle to search (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
isTexture = get(handles.Texturecheck,'Value');
isColour = get(handles.colourCheck,'Value');
if and(isTexture, isColour)
'do something'
elseif isColour
'do something else'
elseif isTexture
'do something else'
else
'warn user'
end
guidata(hObject, handles);

Getting snapshot from webcam in Matlab

I have created a simple GUI to preview webcam stream and to get snapshot from it. For this I have created on axes to show video, one push button(pushbutton1) to start preview, one push button(pushbutton2) to get snapshot. Following is the code for these two push buttons.
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)
axes(handles.axes1);
vidObj = videoinput('winvideo',1);
videoRes = get(vidObj, 'VideoResolution');
numberOfBands = get(vidObj, 'NumberOfBands');
handleToImage = image( zeros([videoRes(2), videoRes(1), numberOfBands], 'uint8') );
preview(vidObj, handleToImage);
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
a=getsnapshot(get(axes,'Children'));
imshow(a);
In pushbutton2_Callback I am trying to get child of axes ie. vidObj. But this gives me error ??? Undefined function or method 'getsnapshot' for input arguments of type 'double'.. Why is it returing double type instead of child object vidObj?
How can I fix it and get snapshot?
Is there any other better way?
(I just started learning GUI.)
Thanks.
A better alternative to declaring your variables global, is to use the handles structure for sharing data. GUIDE already uses this structure to store handles to all GUI components. Simply add your data as a field to this structure that gets passed around to all callback functions.
So inside the first callback:
function pushbutton1_Callback(hObject, eventdata, handles)
%# ... your existing code ...
%# store video object in handles, and persist
handles.vidObj = vidObj;
guidata(hObject,handles)
end
Then in the second, you can retrieve the video object from the handles structure:
function pushbutton2_Callback(hObject, eventdata, handles)
frame = getsnapshot(handles.vidObj);
imshow(frame);
end