How to import Excel data in Matlab GUI?

46 次查看(过去 30 天)
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 个评论
Adam Danz
Adam Danz 2019-9-5
编辑:Adam Danz 2019-9-9
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.

请先登录,再进行评论。

采纳的回答

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

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by