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

6 views (last 30 days)
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 Comment
Adam
Adam on 28 Jun 2017
Edited: Adam on 28 Jun 2017
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.

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 28 Jun 2017
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 Comments
Mark M
Mark M on 8 Jul 2017
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 on 8 Jul 2017
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.

Sign in to comment.

More Answers (0)

Categories

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

Products

Community Treasure Hunt

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

Start Hunting!