I am working on matlab gui. I have to call another gui on clicking a button in current gui. But i have to pass some values like a image and a text string from current gui to the new gui. How can i do this. I have simply open the new gui without passing any information.
function pushbutton8_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton8 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
warning off;
choice = questdlg('Would you really want to goto next window ?', ...
'RESET Dialog', ...
'YES','NO','');
switch choice
case 'YES'
clc;clear all;close all;subpart;
case 'NO'
end
Lets suppose you pass the all the data you want through a struct called myData that is stored inside handles. Then you'd have to simply call
function pushbutton8_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton8 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
warning off;
choice = questdlg('Would you really want to goto next window ?', ...
'RESET Dialog', ...
'YES','NO','');
switch choice
case 'YES'
mySecondGUI(handles.myData);
delete(hObject);
case 'NO'
end
This way your second gui recieves the information. The second thing you have to do is modifying your Openingfunction in your second gui:
mySecondGUID_OpeningFcn(hObject, eventdata, handles, varargin)
handles.theDataFromFirstGUI = varargin{1};
% ... Any initialisationstuff
guidata(hObject,handles);
In case you encounter problems with the old gui not properly closing I suggest you to read the articles regarding uiwait and uiresume: https://www.mathworks.com/help/matlab/ref/uiwait.html
I hope this helps, cheers Pablo
Related
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)
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
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');
I'm kind of new to MATLAB and I'm doing some experiments for a school project.
What I want is a GUI with 3 buttons, when you press either of the first two, it adds up to one on a variable (one variable for each button), and when you press the third button, it does something with the variables from the first two buttons.
I used "guide" and dragged and dropped the buttons, and then I modified the functions.
But I realized that my variables only exist inside the function for the button, so if I initialize them they would restart everytime I press the button, and also there is no way for my third button to know the value of the first two.
Is there a way to make this variables always present? Or pass them from a function to another?
My code it's just the automatic code generated by "guide", with a v1 = v1+1; in the first button callback function and v2 = v2+1 in the second one, and disp(v1) disp(v2) in the third.
I hope you understand what I mean, I'm not a native english speaker so...
Anyway, thanks a lot, hope it's something easy to fix.
You have several options:
use global variables as nhowe suggested. But using global variables is not a good practice: see Top 10 MATLAB code practices that make me cry, or Wikipedia article
use setappdata / getappdata functions to store your variables (this is the simpler one)
learn how to use and properly update the handles structure that appears in each callback function for GUI controls created in GUIDE (this one is more complicated).
Here is an example of *.m file for case #3. Most of GUIDE-generated code was removed showing only things related to your variables. Basically, you have to update the handles structure in each callback function that does some changes to it with guidata(hObject, handles); line. After this all subsequent callbacks will see the updated handles structure.
function varargout = GUIProgramWithVariables(varargin)
% Here goes some comment from GUIDE
% Begin initialization code - DO NOT EDIT
% . . . actual code skipped
% End initialization code - DO NOT EDIT
% --- Executes just before GUIProgramWithVariables is made visible.
function GUIProgramWithVariables_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to GUIProgramWithVariables (see VARARGIN)
% Choose default command line output for GUIProgramWithVariables
handles.output = hObject;
% Here your code starts. It should be at the end of OpeningFcn
% Add your fields to handles structure
handles.C1 = 1;
handles.C2 = 2;
handles.C3 = 3;
% this updates modified handles structure
% so all subsequent call-backs will see the changes
guidata(hObject, handles);
% --- Executes on button press in Button1
function Button1_Callback(hObject, eventdata, handles)
% hObject handle to BrowseButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Here we do the magic with Button1
handles.C1 = handles.C1 + 1;
% this updates modified handles structure
% so all subsequent call-backs will see the changes
guidata(hObject, handles);
% --- Executes on button press in Button2
function Button1_Callback(hObject, eventdata, handles)
% hObject handle to BrowseButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Here we do the magic with Button2
handles.C2 = handles.C2 + 1;
% this updates modified handles structure
% so all subsequent call-backs will see the changes
guidata(hObject, handles);
% --- Executes on button press in Button3
function Button3_Callback(hObject, eventdata, handles)
% hObject handle to BrowseButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Here we do the magic with Button3
handles.C3 = handles.C1 + handles.C2;
% this updates modified handles structure
% so all subsequent call-backs will see the changes
guidata(hObject, handles);
The following is not the best practice for large complicated programs, but for something simple like what you're trying to do it sounds like global variables would be perfect. Say that X, Y, and Z are the variables you want to share between functions. Add the following at the beginning of every function that uses them, and they will all have access to the same values.
global X Y Z
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);