MATLAB Answers

Jiali
0

GUI how to display table data into figure

Asked by Jiali
on 29 Dec 2014
Latest activity Commented on by Geoff Hayes
on 30 Dec 2014
My problem is: I try to read some data from excel file, first display as table data inside Matlab GUI, then I want to display data in the figure (GUI). I know I can re-read the excel data again to display in the figure. But how I can get table handle and transfer to figure as data input? I am a beginner in GUI, please help me. I highly appreciate it.
function uitable1_CreateFcn(hObject, eventdata, handles)
[num,txt,raw]=xlsread('C:\Program Files\data_example.xls');
set(hObject,'data',num,'ColumnName',txt);
handles.uitable1=hObject;
% guidata(hObject,handles);
function axes1_CreateFcn(hObject, eventdata, handles)
tab_data=get(handles.uitable1,'Data') %%%%%always tell me wrong "Attempt to reference field of non-structure array." %%%%
handles.axes1=plot3(tab_data(:,1),tab_data(:,2),tab_data(:,3));
guidata(hObject,handles.axes1);

  0 Comments

Sign in to comment.

Products

2 Answers

Answer by Geoff Hayes
on 30 Dec 2014
 Accepted Answer

Jiali - the error message is telling you that the handles input parameter is not a structure. If I create a simple GUI with an axes and uitable, define the createFcn for both and run the GUI, then when the createFcn is called, the handles structure is empty for both controls. I suspect that the same is true for you too.
Typically, I don't use the createFcn for controls and instead place all of my initialization code in the OpeningFcn of the GUI. This function is called after all the createFcn calls have been made for each control. For your case, you could then read in the data from the Excel file and use it to populate both the table and the axes. Something like
function untitled_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for untitled
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% read the data from file
[num,txt,raw]=xlsread('C:\Program Files\data_example.xls');
% update the table
set(handles.uitable1,'data',num,'ColumnName',txt);
% update the plot
plot3(handles.axes1, num(:,1), num(:,2), num(:,3));
Note how easy it is to update the two controls. Try it and see what happens!
A couple of things concerning your original code:
handles.uitable1=hObject;
is unnecessary since hObject is identical to handles.uitable1. I also recommend that you never do this (modifying graphics handles) because that will lead to unexpected/undesired behaviour in your code.
And
handles.axes1=plot3(tab_data(:,1),tab_data(:,2),tab_data(:,3));
guidata(hObject,handles.axes1);
Again, don't do this. You are replacing the handle to the axes control with the handle to the plot graphics object. The call to guidata will then destroy the handles structure, replacing it with a graphics object handle. If you want to save the handle to the plot, then do instead
handles.hPlot3 = plot3(tab_data(:,1),tab_data(:,2),tab_data(:,3));
guidata(hObject,handles);
Check the call to guidata - we just pass handles as the second input parameter.

  2 Comments

Hi Geoff Hayes,
You got all my points.I check that handles structure is empty for both controls in my original code, just as you mentioned before.
And I tried your code. It works very well and you always tell me why. I appreciate your suggestion, especially for the cautions you reminded me.
Thanks a lot. Have a good new year!
Have a good new year too!

Sign in to comment.


Answer by Khanh
on 30 Dec 2014

You have to set data for the table before retrieve it.
[num,txt,raw]=xlsread('C:\Program Files\data_example.xls');
% Set data table (num variable is data you want to set)
set(handles.uitable1,'Data',num)
% And then get data table
tab_data=get(handles.uitable1,'Data')
Hope this can help you.

  0 Comments

Sign in to comment.