Plot data import from excel to figure use GUI ?

조회 수: 1 (최근 30일)
Le Dung
Le Dung 2018년 12월 17일
댓글: Le Dung 2018년 12월 20일
Hello everyone!
I'm a beginer. and i want to study on GUI.
Now, i want to build a GUI such as: (see picture)
First, i want to click on "Import" button to import data from a excel file. And, i choise plot my data in "linear" or "dB", (1 dB = 20*log(x)), x is input data.
And, after i decided data in Y axes (vertical axes) is linear or dB depend on i click on "linear" or "Log". i will click on "draw" button to draw my data.
I wrote a code, but, it didn't run.
Please help me!
Thank you so much
This is my code: (i only show Callback functions)
function import_Callback(hObject, eventdata, handles)
handles.fileName = uigetfile('*.xlsx'); % imput mot file Excel
guidata(hObject,handles);
function draw_Callback(hObject, eventdata, handles)
filename = get(handles.import,'String');
values = xlsread(filename);
xCol = values(:,1);
yCol = values(:,2);
set(handles.truc,'Visible','on');
plot(handles.truc,xCol,yCol)
function linear_Callback(hObject, eventdata, handles)
set(handles.truc,'Scale','Linear');
function log_Callback(hObject, eventdata, handles)
set(handles.truc,'Scale','Log');
  댓글 수: 4
Le Dung
Le Dung 2018년 12월 17일
Yes. When i click on "Run" to run this code.
Program will run, and, i click on "Import" button. then, i can choise a file excel because i used a uigetfile command. and i cilck on open this excel file.
And then, i click on "linear" or "log". And finally, i click on "Draw" button, matlab return:
Error using xlsread (line 128)
XLSREAD unable to open file 'Import'.
File 'Import' not found.
Error in vefrf>draw_Callback (line 89)
values = xlsread(filename);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in vefrf (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)vefrf('draw_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
I think may be "xlsread" command didn't realized filename variable.
Untitled.png
Luna
Luna 2018년 12월 17일
Are you sure on that line values = xlsread(filename); filename is the full file name with its path like: 'C:\users\....\example1.xls' ?

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

채택된 답변

Geoff Hayes
Geoff Hayes 2018년 12월 17일
The problem may be with this code
function draw_Callback(hObject, eventdata, handles)
filename = get(handles.import,'String');
values = xlsread(filename);
xCol = values(:,1);
yCol = values(:,2);
set(handles.truc,'Visible','on');
plot(handles.truc,xCol,yCol)
Is handles.import the handle to your Import button? And so when you read the string (from it) you are then trying to open a file named "Import"? The import_Callback saves the name of the file to handles.fileName so your above code should be
function draw_Callback(hObject, eventdata, handles)
if isfield(handles, 'fileName')
filename = get(handles.fileName);
values = xlsread(filename);
xCol = values(:,1);
yCol = values(:,2);
set(handles.truc,'Visible','on');
plot(handles.truc,xCol,yCol)
end
As Luna has indicated, make sure that handles.filename includes the full path to your file. You may need to change the import callback code to
[file, path] = uigetfile('*.xlsx');
handles.fileName = fullfile(file, path);
guidata(hObject,handles);
  댓글 수: 1
Le Dung
Le Dung 2018년 12월 20일
yes. exactly, i want to get file name of the file excel that took into import_callback function.
and, my problem is using variables through callback functions

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

추가 답변 (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