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

7 ビュー (過去 30 日間)
Matt
Matt 2017 年 6 月 28 日
コメント済み: Mark M 2017 年 7 月 8 日
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 Center および File ExchangeMigrate GUIDE Apps についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by