How to import Excel data in Matlab GUI?

조회 수: 63 (최근 30일)
Enrica Brunetti
Enrica Brunetti 2019년 9월 5일
댓글: Jacks Thawn 2020년 12월 23일
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
Bhargavi Maganuru 2019년 9월 9일
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
Jacks Thawn
Jacks Thawn 2020년 12월 23일
may i see the complete script for this please

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by