How to load data into uitable via button in GUI using GUIDE

12 次查看(过去 30 天)
I'm currently creating a GUI using GUIDE (which I am relatively new to). I would like to be able to get the end user to select a .csv file to load into the GUI and for it to be displayed in a table. So far, I am using the following code:
function loadBtn_Callback(hObject, eventdata, handles)
% hObject handle to loadBtn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename, pathname]=uigetfile({'*.csv'}, 'Select File');
if isequal(filename,0)
return
else
Path=strcat(pathname,filename);
data=readtable(Path, 'Delimiter', ';');
set(handles.data_table, 'Data', data);
end
guidata(hObject, handles);
However, I keep getting the following error:
Error using matlab.ui.control.Table/set
While setting the 'Data' property of 'Table':
Data must be a numeric, logical, or cell array
I've tried running this same code in a normal script and it works fine, what am I doing wrong?
  1 个评论
Adam
Adam 2017-6-28
编辑:Adam 2017-6-28
readtable
reads into a table data structure object, but unfortunately these (being relatively recent additions) are not supported by uitable so you have to load your data into a cell array or numeric array to feed it to uitable.
I never use csv files or tables though so someone else can hopefully advise better on how to get the data loaded in.
doc csvread
should give you a numeric matrix I think.

请先登录,再进行评论。

采纳的回答

Geoff Hayes
Geoff Hayes 2017-6-28
Matt - readtable returns a table which cannot be used to set the data within a uitable (as the error message indicates). Depending upon the data in your table, you can try converting it to a cell array or numeric array using table2cell or table2array respectively. Or, you can investigate using a different method to import your data from file. See Ways to Import Text Files for details.
  3 个评论
Mark M
Mark M 2017-7-8
Hello,
I am attempting to do something similar, but as a listbox. When the user selects an option in the listbox, I want to pull only a certain set of data from the Excel file into the uitable in my GUI. Is this possible?
Mark M
Mark M 2017-7-8
I figured it out in my case. For those interested, since I already had the dataset I wanted loaded in earlier in my code, I simply needed to use the table2cell as shown:
a = get(handles.TuscSortStreetRating,'Value'); if (a==1) figure imshow('TestImage.jpg') street1 = 'TestBook.xlsx'; T = readtable(street1); C = table2cell(T);
data=(C);
set(handles.uitable1, 'Data', data);
guidata(hObject, handles);
Once the user selects the first option of the listbox, this pops up an image and loads in the requested data from the test excel file.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by