i have problem with matlab gui that when i set it global variable not work and not shown in work space table
also i would to use it in more than function how to use it i tryed this code but not worked
the idea that i set poin x , y , z then i want to made offset with this points so i need to use it as global variables
function pushbutton2_Callback(hObject, eventdata, handles)
global Ref_x;
global Ref_y;
global Ref_z;
px=str2double(get(handles.pos_x,'string'))+Ref_x;
py=str2double(get(handles.pos_y,'string'))+Ref_y;
pz=str2double(get(handles.pos_z,'string'))+Ref_z;
Related
I am struggling to use axes on my GUI because it says it is not defined. Here is the summary of the code :
function bitirme_OpeningFcn(hObject, eventdata, handles, varargin)
axes(handles.axes1)
imshow('photo1.jpg');
...
function pushbutton1_Callback(hObject, eventdata, handles)
theta=inverseKinematic(...)
...
function [Theta1,Theta2]=inverseKinematic(angle1,angle2,angle3,desCorX,desCorY)
axes(handles.axes1);
....
plot(a,b);
....
Until the function inverseKinematic is called,everything works fine. However when it is called, the image doesn't turn to be a graphic and I see the error " Undefined variable "handles" or class "handles.axes3" " on matlab command window. Why can I not call axes(handles.axes1) in a nested function? What is the reason behind it?
Each function has its own workspace, and callbacks generated by GUIDE share data associated with what is called the handles structure. Every components of the GUI (axes, pushbuttons, etc.) are associated with the figure in which they are located. Hence GUIDE already provides the callback functions with everything stored in the handles structure.
However, nested functions do not have implicit access to the data stored in the handles structure. You need to specifically provide those data to the functions. There are a couple options available, for example:
1) Provide the handles structure as an input argument to the function.
2) Use getappdata and setappdata functions to associate data with a figure or MATLAB's root
3) Retrieve the handles structure "by hand" at the very beginning of the nested functions like so for example:
function [Theta1,Theta2]=inverseKinematic(angle1,angle2,angle3,desCorX,desCorY)
handles = guidata(gcf)
axes(handles.axes1);
....
plot(a,b);
....
%// Save the changes made to the handles structure.
guidata(handles to the figure,handles);
where gcf refers to the current figure. You can actually use the name of the figure or hObject.
Note that when using the command plot, you don't need to set the current axes before each call. You can use the Parent property inside the call to plot to select where to plot the data.
Example:
plot(a,b,'Parent',handles.axes1)
I hope that was clear enough!
I'm making an RGB image filter with a GUI where I change certain parameters through a slider in the GUI. I then want to save the image through a pushbutton in the GUI but I can't. Here's a quick insight:
function slider5_Callback(hObject, eventdata, handles)
im = imread('C:.........jpg');
C = im;
end
function pushbutton1_Callback(hObject, eventdata, handles)
imshow(C);
imsave
end
How can I solve this?
It's not the best option, but you can use global variables.
Declare 'C' as global C; in the function you read the file.
Write global C; in the function you want to use. It will be able to use it.
I created a GUI to set parameters and use those parameters in another m-File. I know that the variables created in GUI are in a different workspace from the input variables to the m-File. For that reason, I'm trying to use global variables, but not sucesfully: the variables goes to the m-File empty.
I have a GUI which has 3 edit boxes, that the user can set parameters (0 or 1), using this code (showing the code for only one edit box):
function density_Callback(hObject, eventdata, handles)
% hObject handle to density (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 density as text
% str2double(get(hObject,'String')) returns contents of density as a double
global density
density = str2double(get(hObject, 'String'));
global Variavel_1
assignin('base', 'Variavel_1', density);
The m-File are called from a GUI BUTTON, using this code:
function calculate_Callback(hObject, eventdata, handles)
% hObject handle to calculate (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
test %m-File that uses variables set in GUI
And the m-File should use this Variavel_1 and the others from the remaining editboxes in this code (test.m):
global Variavel_1
global Variavel_2
global sum
Variavel_1
Variavel_2
sum = Variavel_1 + Variavel_2
But all I have is all those variables empty!
(I know I can put this code inside the GUI code, but that's not the real propouse because this simple m-File to sum will be much more complex, so I need the variables to go to a m-File).
Variavel_1 = []
Variavel_2 = []
Sum = []
Can anyone understand why the global variables fails?
Thanks in advance!
In auto generated OpeningFcn callback, if you're using GUIDE
You can define variables/fields in handle structure.
This is initialization part. The data used by this can be shared across all widgets on the GUI.
So, in this callback, for variable_1,variable_2, sum, etc. you can have something like following:
handles.variable_1 = 1;
handles.variable_1 = 2;
handles.sum = handles.variable_1 + handles.variable_1 = 1;
% At end update handles structure
handles.output = hObject;
guidata(hObject, handles);
In other callback, say your Button or Edit callback
you can update these values as normal like following
handles.variable_1 = 12;
handles.variable_1 = 23;
handles.sum = handles.variable_1 + handles.variable_1 = 1;
% Now, at end of callback save the change you made to the structure
guidata(hObject,handles)
To verify these values type handles at command window.
Ref :Store or retrieve GUI data for more details.
To use these variables from GUI to other script simple assign them in workspace
like assignin('base','variable_1',handles.variable_1); ,etc in any of the callback where variable_1 is getting updated.
Then, in your m-script you can directly use these variables.
I think you could just define your test function (saved in "test.m") as:
function sum = test(x, y)
sum = x + y;
And then call your test function from your calculate_CallBack function using:
sum = test(Variavel_1, Variavel_2);
Admittedly, I am not well versed in GUI programming in MATLAB, so there may be some really obvious reason for which you cannot do this, but this avoids using global variables, which often makes for cleaner code.
I created a GUI with many pushbuttons. I name a push button "Refresh". What I want to do is when I hit this button, update the variables of the workspace in the central Matlab window by running a m-file that makes all the changes (opens files gets data and puts them in cell arrays and when running again refreshes the data).
I tried everything possible found online like using the name of the m-file under the pushbutton callback function but it doesn't work.
This is the code of my pushbutton function and I added the myparameter.m in it:
function pushbutton1_Callback(hObject, eventdata, handles)
myparameters
But when I run it the variables produced from the m-file are not saved in workspace. If I write myparameters in the main matlab window all the variables created in the m-file are saved in the workspace.
To assign variables in the base workspace from a function, you need to use the assignin function; e.g.,
assignin('base', 'myVariableName', myVariableValue);
Assuming myparameters is a script (it would have to be to work as described), then you should be able to evaluate it in the base workspace using evalin. Here's what your callback will look like:
function pushbutton1_Callback(hObject, eventdata, handles)
evalin('base', 'myparameters');
end
I've written a Matlab script that reads in data using a virtual COMM port in real-time. I've done a significant amount of signal processing in an mfile.
Next, I felt the need to have a compact GUI that displays the information as summary.
I only recently started digging and reading more of Matlab's built-in GUI tool, GUIDE. I've followed a few tutorials and am successfully able to get my graphs to display on my GUI after a button-press.
However, I want the GUI to update in real-time. My data vector is constantly updating (reading in data from the COMM port). I want the GUI to keep updating the graphs with the newer data, as opposed to relying on a button press for an update. Can someone please point me in the right direction for background updating?
Here is the relevant code currently for the GUI:
% --- 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)
global data
global time
% Time domain plot
axes(handles.timeDomainPlot);
cla;
plot (time, data);
EDIT Changed code:
% --- 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)
%Setting it to display something when it ends
% t = timer('TimerFcn', 'timerOn=false; disp(''Updating GUI!'')',...
t = timer(...
'TasksToExecute', 10, ... % Number of times to run the timer object
'Period', 3, ...
'TimerFcn', GUIUpdate());
%Starting the timer
start(t)
function GUIUpdate()
global data
global time
%Parameters below axes
global min
global max
% Time domain plot
axes(handles.timeDomainPlot);
cla;
plot (time, data);
%Other parameters:
set(handles.mean, 'String', mean);
set(handles.max, 'String', max);
The error that I get is:
??? Error using ==> GUI_Learning>GUIUpdate
Too many output arguments.
Error in ==>
#(hObject,eventdata)GUI_Learning('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
Here is an example using a timer with a timerFcn callback. I made a simple GUI with 1 axes and 1 button.
In the opening function I initialize the plot and create the timer. In the start button callback I start the timer and start manipulating the data. The timer function callback the just updates the y-data of the line via its handle. Below are the relevant functions from the GUI's M-file (snipped init section and output fcn.
function testTimer_OpeningFcn(hObject, eventdata, handles, varargin)
global y x
x = 0:.1:3*pi; % Make up some data and plot
y = sin(x);
handles.plot = plot(handles.axes1,x,y);
handles.timer = timer('ExecutionMode','fixedRate',...
'Period', 0.5,...
'TimerFcn', {#GUIUpdate,handles});
handles.output = hObject;
guidata(hObject, handles);
% --- Executes on button press in startButton.
function startButton_Callback(hObject, eventdata, handles)
global y x
start(handles.timer)
for i =1:30
y = sin(x+i/10);
pause(1)
end
function GUIUpdate(obj,event,handles)
global y
set(handles.plot,'ydata',y);
You may want a Stop button to stop the timer depending on how your GUI is structured and were/how the data is updated.
Edit: Basic handles info some of this is pretty basic and you may already know it:
An individual handle to an object contains a bunch of properties that you can read with the get() function or set with the set() function. So for example maybe I wanted to change the text of the startButton for some reason in my GUI.
set(handles.startButton,'String','Something Other Than Start');
You may just want to set a break point in your code somewhere (maybe in a button press) and play around with the handles struct. Running get() commands on various objects to learn their properties.
Now the handles structure contains all of the ... umm... handles to your GUI's objects as well as any custom items that may be convenient for your to store there. Most GUI callbacks automatically get passed the handles struct so you have easy access to all parts of the GUI.
Ex. The 'startButton' callback was automatically passed handles. So I had easy access to the timer object via handles.timer.
Which brings me to sticking custom things into handles. In the opening function I added a new item to the handles structure handles.timer and handles.plot because I knew they would be useful in other callbacks (like button press and the timerFcn callback).
However, to store these things permanently you need to use the 'guidata' function. This function basically either stores the modified handles struct or retrieves a copy of handles depending on how you call it. So the following line in the opening function is storing the modified handles structure (added .timer and .plot) into the main GUI.
guidata(hObject,handles);
Basically any time you add something in handles you should have that line to make the change permanent.
Now the other method of calling it is:
handles = guidata(hObject); %hObject can be any handle who is a child of the main GUI.
This will retrieve the handles structure for the GUI.
And last handles.output = hObject is just the default output when you launch your GUI. IF you call your GUI via Matlab's command line like this h = myGUI; it should return the handle to your GUI.
You need to use a timer object. Set the callback to be the function that updates the plots.
Take a look at Making Graphs Responsive with Data Linking
and the linkdata command.
If the same variable appears in plots in multiple figures, you can
link any of the plots to the variable. You can use linked plots in
concert with Marking Up Graphs with Data Brushing, but also on their
own. Linking plots lets you
Make graphs respond to changes in variables in the base workspace or within a function
Make graphs respond when you change variables in the Variable Editor and Command Line
Modify variables through data brushing that affect different graphical representations of them at once
Create graphical "watch windows" for debugging purposes
Watch windows are useful if you program in the MATLAB language. For
example, when refining a data processing algorithm to step through
your code, you can see graphs respond to changes in variables as a
function executes statements.
I made a quick and dirty test seen below and I am not sure how this will work in a GUI verses a function but may do the trick.
Note 1: I had to add a break point in my subroutine where it modifies the global y to actually see the plot auto-update. You may need some combination of drawnow, pause, or a timer if data is getting changed rapidly.
function testLinking()
global x y
%Links failed if the global did not also exist in the base workspace
evalin('base','global x y');
x = 0:.1:3*pi; % Make up some data and plot
y = sin(x);
h = plot(x,y,'ydatasource','y','xdatasource','x');
linkdata on
testSub
function testSub()
%Test to see if a sub can make a linked global refresh
global x y
for i = 1:10
%This should automatically update the plot.
y = sin(x+i/10);
end
Edit: there may be ways around the use of globals depending on how your functions are structured ... but I don't have time to dig into it to much.
You can add a callback on the serial object that executes a plotting function. You must attach the callback to the 'BytesAvailableFcn' event on the object (see this for more details on the properties of the com object).
Essentially, when there are bytes available on the com port, you instruct matlab to run a specific function. In your case, it will be the function updating the GUI. If you need to process the incoming data first, then your callback function will first do the signal processing and then do the plotting commands.