How to import Excel data in Matlab GUI?

57 Ansichten (letzte 30 Tage)
Enrica Brunetti
Enrica Brunetti am 5 Sep. 2019
Kommentiert: Jacks Thawn am 23 Dez. 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 Kommentar
Adam Danz
Adam Danz am 5 Sep. 2019
Bearbeitet: Adam Danz am 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Bhargavi Maganuru
Bhargavi Maganuru am 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 Kommentar
Jacks Thawn
Jacks Thawn am 23 Dez. 2020
may i see the complete script for this please

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by