Im using GUIDE to make a matlab GUI which does some video computation.
Using the preview function I can preview the live video from my webcam and do some calculations.
In the MainGUI i use the:
setappdata(hImage,'UpdatePreviewWindowFcn',mypreview_fcn);
to get to a custom preview function which is:
function mypreview_fcn(obj,event,himage)
originalframe=peekdata(vidobj,1);
while isempty(originalframe)
originalframe=peekdata(vidobj,1);
end
if kk>=1
[LogResult,y,dist]=QueryArduino;
if LogResult==1
kk=kk+1;
results{kk,1}=originalframe;
results{kk,2}=measure1;
results{kk,3}=measure2;
results{kk,4}=measure3;
results{kk,5}=measure4;
results{kk,6}=measure5;
results{kk,7}=measure7;
offset=median([results{:,7}]);
offset=measure2-measure3;
end
end
set(himage,'CData',originalframe);
end
I would like to pass the result matrix to a table in the MainGUI
How can I access the GUI table;
You could do it using getappdata to pass all the data you want inside MainGUI and populate the table. Actually I don't understand why you would use setappdata to call a function.
For instance, in the function mypreview_fcn you coudl write something like this right before the end of the function:
setappdata(0,'MyData',results);
and then in MainGUI use getappdata:
TableData = getappdata(0,'MyData');
set(HandletoTable,'Data',TableData);
An alternative (and better approach I think) to getappdata would be to assign an output argument to the function mypreview_fcn so that when you call it from mainGUI the variable results is recognized inside mainGUI and you can populate the table simply using
set(HandletoTable,'Data',results);
Is this what you meant?
Related
I'm creating a uitable in Matlab Guide that generates file names automatically based off of several parameters that a user will enter. For each file name in the table, a run time and number of runs can be specified.
I'm trying to write a callback function for the uitable that will automatically update the run names when either the run time or number of runs is edited. This is the callback function I've written.
function runTableCallback(hObject,callbackdata)
numval = eval(callbackdata.EditData);
r = callbackdata.Indices(1);
c = callbackdata.Indices(2);
if c==2
handles.runnums(r,1)=numval;
hObject.Data{r,c} = numval;
elseif c==3
handles.runtimes(r,1)=numval;
hObject.Data{r,c} = numval;
end
[runlog,runnames,runnums,runtimes,rundesc]=generateRuns(hObject,handles);
handles.runlog=runlog;
handles.runnames=runnames;
handles.runnums=runnums;
handles.runtimes=runtimes;
handles.rundesc=rundesc;
set(handles.uitable19,'Data',[handles.runlog,handles.runnames,handles.runnums,handles.runtimes]);
guidata(hObject, handles);
I need to call the 'generateRuns' funtion in order to create the updated table information with the edited data. Then that updated information will be displayed in the table using the "set" function on the next line. However, I am getting the following error:
Undefined function or variable "handles".
How can I access handles within the callback function? The callback function is specified in the UI Controls for the table in another object callback function.
set(handles.uitable19,'CellEditCallback',#runTableCallback);
Any help would be much appreciated.
You have not passed handles to your callback.
Try adjusting your set call to this:
set(handles.uitable19,'CellEditCallback', {#runTableCallback, handles});
And your function definition to:
function runTableCallback(hObject, callbackdata, handles)
I have being to share data between several callback options in Matlab, however no succes so far. I have a gui with multiple tables which I use to get the input from the user. I have multiple callback functions for the different tables. I would like to use the data from table 1 and callback 1 together with the date in table 2 in callback two.
function MaterialProperties(Material, Data)
Material_data = get(Material, 'Data');
% Avoid bluehighlight in table
set(Material,'Data',{'dummy'});
set(Material,'Data', Material_data);
% Store variable in workspace
assignin('base','Material_data',Material_data)
%Mat_data = guidata(gcbo);
%for i=1:size(Material_data,2)
% Mat_data.MatData{i}=Material_data{i};
%end
% Save the change you made to the structure
guidata(gcbo,Mat_data)
assignin('base','Mat_data',Mat_data)
end
function Stacking_sequence(Layup, Data)
% I want to use the date (Material_data) of MaterialProperties here in this callback
layup_data = get(Layup, 'Data');
%overwrite data with a dummy and restore the old data afterwards, to force deselection
set(Layup,'Data',{'dummy'});
set(Layup,'Data', layup_data );
%store variable in workspace
assignin('base','layup_data',layup_data)
layup = strsplit(layup_data{1,1},'\');
assignin('base','layup',layup)
end
Can anyone help. I tried theMatlab help, but the suggestions stated there didn't work (maybe I implemented it wrong)
It looks like you simply need to retrieve the handles structure at the beginning of callback 2, like you did in the first callback:
Mat_data = guidata(gcbo);
after which it should be available in the 2nd callback. By the way this very line and the 3 lines following it are commented in your code is that a mistake?
Alternative solution:
As an alternative solution, you can use setappdata/getappdata to share data between function callbacks as well as in the command window, depending on where you store those data.
For example, if you save Material_data at the end of the 1st callback using something like this:
setappdata(0,'MatData',Material_Data); % Save in the Matlab root 0 (accessible everywhere), and give some dummy name)
Then at the beginning of the 2nd callback, you can retrieve the data using getappdata:
Material_Data = getappdata(0,'MatData');
and you're good to go. Instead of using the 0 root, you could also store the data in the GUI itself, using for example handles.FigureGUI or whatever the name of the figure is. Then the data would be available only if the figure is not closed/deleted. Play around with those and see what you prefer.
Hope that helps!
In this post,Matlab: How to get the current mouse position on a click by using callbacks , it shows something as follows:
function mytestfunction()
f=figure;
set(f,'WindowButtonDownFcn',#mytestcallback)
function mytestcallback(hObject,~)
pos=get(hObject,'CurrentPoint');
disp(['You clicked X:',num2str(pos(1)),', Y:',num2str(pos(2))]);
However, I cannot get the pos and use it in mytestfunction().
could anyone help ? Thanks !
If you are not using GUIDE and hence do not have the handles structure (see bottom), you can utilize the UserData property of the figure to pass any information:
set(gcf,'UserData',pos);
Then you can access pos from anywhere else via:
pos = get(gcf,'UserData');
See this MathWorks description of the UserData property, and this full example. From the first page:
All GUI components, including menus and the figure itself have a UserData property. You can assign any valid MATLAB workspace value as the UserData property's value, but only one value can exist at a time.
As a workaround to this limitation, you could assign a struct to UserData that has all the properties needed by your program stored in different fields.
A detail I left out in the commands above is the figure/object handle (you probably don't actually want to use gcf). In mytestfunction you have it stored in f. In the callback you can find the parent figure of the hObject by:
f = ancestor(hObject,'figure');
One way to use the above approach is to simply wait for changes in the UserData property:
function mytestfunction()
f=figure; set(f,'WindowButtonDownFcn',#mytestcallback)
maxPos=10; cnt=0;
while cnt<maxPos, waitfor(f,'UserData'); pos=get(gcf,'UserData'), cnt=cnt+1; end
function mytestcallback(hObject,~)
pos=get(hObject,'CurrentPoint');
set(ancestor(hObject,'figure'),'UserData',pos);
Note that another way to handle events is to implement a listener to respond to the clicking event, but the WindowButtonDownFcn callback should work fine.
Originally, I was thinking GUIDE, in which you would have the handles structure. This is one purpose of the handles structure. Store the position in a field of handles, and update it:
handles.pos = pos; % store it
guidata(hObject,handles); % update handles in GUI
Then back in mytestfunction or whatever other callback needs access to pos, you can use handles.pos, if the handles structure is up to date.
I have program.m file that contains:
for i=1:k
V=geAllThreeElements(n);
fprintf('total is %d\n',size(V,1));
end
and I have tst.fig that contain Push Button that call program function and listox
I want print ('total is %d\n',size(V,1)) in list box not in command windows during program function execution, How?
Thanks
If I got you right, you are calling a m-file (script) from a pushbutton within a figure.
First of all you should return the Data of the variable you called "V". If you dont use programm.m as a function and you totally dont want to do that, you have to get "V" by loading it into your the workspace of your GUI.
See assignin for example!
now that you have the Data of V, you can use it to display information about it within a listbox like this:
%you have to know the handle of the listbox-> I call it myLB:
%if you want to append data:
myData = get(myLB, String)
set(myLB, String, [myData sprintf('total is %d\n',size(V,1))])
%if you want to replace data:
set(myLB, String, sprintf('total is %d\n',size(V,1)))
I have a uitable in MATLAB and currently I have callback functions to every cell.
I have been trying for a while now to set a callback to the column and row name, but with no success. More specifically if the user clicks on a particular column name, is it possible to call a function?
Please let me know if you need any more info... I would appreciate any help.
Thanks in advance!
First of all, you have to register callback to table header object. It is JTableHeader object, and you can access with findjobj function.
I created demo for registering callback function for column name click event.
This callback function is used to modify the clicked column name. It is tested on Matlab R2015a.
To run this demo, download findjobj file, and put in the same folder. Then run below code.
function TableDemo()
% Demonstration of clickable columnname.
% In this example, we use the click event to modify column name.
figure('menubar','none','numbertitle','off', 'Name', 'DEMO');
myTable = uitable('Data', magic(4), 'ColumnName',{'A','B','C','D'}, 'ColumnWidth',{50});
% Accessing underlying java object.
jscrollpane = findjobj(myTable);
jtable = jscrollpane.getViewport.getView;
jheader= jtable.getTableHeader(); % Here, you got JTableHeader object.
h=handle(jheader, 'callbackproperties');
% Set a matlab function as MouseClickedCallback
set(h, 'MouseClickedCallback', {#onHeaderClick, jtable, myTable});
end
function onHeaderClick(src, evt, jtable, hTable)
if(get(evt, 'ClickCount') > 1)
disp('header double clicked');
% Get view index from current mouse point, and convert it to
% model index. Then add 1 because Matlab index starts from 1.
index = jtable.convertColumnIndexToModel(src.columnAtPoint(evt.getPoint())) + 1;
prompt={'Column Name'};
title='Enter column names';
numLines=1;
defaultAnswer=hTable.ColumnName(index);
options.Resize='on';
options.WindowStyle='modal';
newName=inputdlg(prompt,title,numLines,defaultAnswer,options);
if ~isempty(newName)
hTable.ColumnName(index) = newName;
end
end
end
This seems to be impossible using only the standard MATLAB interface to uitable.
However, you can access the underlying Java JTable as described on undocumentedmatlab.com. Using the JTable instance you should be able to install an appropriate event handler, see this question on SO and this other article on undocumentedmatlab.com.