How to import Excel data in Matlab GUI?

61 views (last 30 days)
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
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.

Sign in to comment.

Accepted Answer

Bhargavi Maganuru
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.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!