How can I output variables from a GUI to the workspace in MATLAB 7.8 (R2009a)?

23 views (last 30 days)
I have created a simple GUI in GUIDE with a uitable. Each row in the table represents a vector, with each column being 'x',y', or 'z' coordinate of a vector.
To specify the UITABLE, with my GUI open in GUIDE, I right click on the UITABLE and select View Callbacks->CreateFcn. I add the following to the uitable's Create Function:
% create 2x3 uitable and initialize each cell to 1
set(hObject, 'Data', num2cell(ones(2,3)))
% name the columns and rows in the uitable
set(hObject, 'RowName', {'1st vector', '2nd vector'}, 'ColumnName', {'x', 'y', 'z'});
% set the columns to be editable
set(hObject, 'ColumnEditable',logical([1,1,1]));
I want to compute a dot product between the two vectors in uitable when the cell in the uitable is edited, and store the result in the workspace. How can I do this?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 26 Aug 2013
There are different ways in which you can send output to the base workspace from a GUI.
You can make use of the output function of the GUI. For example, when you create a GUI with the name ‘mygui’ using GUIDE and save it, GUIDE will automatically create a ‘mygui.m’ file. One of the sub-functions in ‘mygui.m’ will be the ‘main_OutputFcn’ which can be edited to send meaningful output to the workspace. Please refer to the demo video in the following webpage for more information:
<http://blogs.mathworks.com/videos/2010/02/12/advanced-getting-an-output-from-a-guide-gui/>
In order to pass the required variable to the output function, you can make use of the SETAPPDATA and GETAPPDATA. Please refer to the documentation pages for the above functions for additional information about their usage.
With your GUI open in GUIDE, right click on the UITABLE and select View Callbacks->CellEditCallback.
Add the following lines to the uitable's Cell Edit Callback function:
% This saves Data from the UITABLE into cell array A
A=get(hObject,'Data');
% This creates double array from a cell array A
B=cell2mat(A);
% This calculates a dot product between the 2 vectors
dotPr=dot(B(1,:),B(2,:));
% This Creates a variable called ‘result’ (if it does not exist) and assigns it the value of ‘dotPr’. The variable ‘result’ is stored as the application data of the GUI.
Setappdata(hObject,result, dotPr);
Modify the output function ‘main_OutputFcn’ in the following manner to return ‘result’ to the workspace:
% --- Outputs from this function are returned to the command line.
function varargout = main_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get ‘result’ and send it as the command line output for this GUI
varargout{1} = getappdata(hObject,result);
An alternative solution is to use the function ASSIGNIN to output the data from GUI to workspace. Refer to the corresponding documentation page for more information about the function.
If using ASSIGNIN, you can modify the ‘CellEditCallback’ of the UITABLE as follows:
% save Data from the UITABLE into cell array A
A=get(hObject,'Data');
% create double array from a cell array A
B=cell2mat(A);
% calculate a dot product between the 2 vectors
dotPr=dot(B(1,:),B(2,:));
% assign the value of the dot product to the variable 'result' in the workspace
assignin('base','result',dotPr)
You might find the following documentation pages about data management in a GUIDE GUI and a Programmatic GUI useful:
<http://www.mathworks.com/help/releases/R2013a/matlab/creating_guis/ways-to-manage-data-in-a-guide-gui.html>
<http://www.mathworks.com/help/releases/R2013a/matlab/creating_guis/ways-to-manage-data-in-a-programmatic-gui.html>

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products


Release

R2009a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!