How to import Excel data in Matlab GUI?
16 views (last 30 days)
Show older comments
Enrica Brunetti
on 5 Sep 2019
Commented: Jacks Thawn
on 23 Dec 2020
I want to create a Push Botton with GUI and it loads an Excel file. I use this code:
function pushbutton1_Callback(hObject, eventdata, handles)
handles.fileName = uigetfile('*.xlsx');
guidata(hObject, handles);
fileName = handles.fileName;
But when I choose the Excel file, it doesn't load the data, I don't see them in the workspace.
Moreover if I want to create the plot of those data, I use plot(fileName)?
1 Comment
Adam Danz
on 5 Sep 2019
Edited: Adam Danz
on 9 Sep 2019
The only thing your code is doing is...
... using an interface so the user can select a file
handles.fileName = uigetfile('*.xlsx');
...saving the handles to the button object (which isn't necessary to do)
guidata(hObject, handles);
...reassigning a variable stored in handles.fileName to a new variable named fileName.
fileName = handles.fileName;
The code isn't loading any data. Even if you did load data, you'd have to do something with that data within that function. When the callback function completes, that workspace is no longer available.
Accepted Answer
Bhargavi Maganuru
on 9 Sep 2019
You can use following code to load and plot the data from excel file.
function pushbutton1_Callback(hObject, eventdata, handles)
handles.filename=uigetfile('*.xlsx');
guidata(hObject,handles);
filename=handles.filename;
values=xlsread(filename);
x=values(:,1);
y=values(:,2);
plot(x,y);
handles.xvalues=x;
handles.yvalues=y;
guidata(hObject,handles);
Function xlsread loads the data, but when the callback function completes, that workspace is no longer available.
If you want to access variables, you can save them to the handles structure, so that other callback functions can access these variables. Update handles structure using guidata.
1 Comment
More Answers (0)
See Also
Categories
Find more on Data Import from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!