Unable to Plot from Excel

조회 수: 3 (최근 30일)
Avinav Kumar
Avinav Kumar 2020년 10월 12일
댓글: Walter Roberson 2020년 10월 12일
I am unable to plot from Excel Sheet. My code is s follows. If someone can please help
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[file, path] = uigetfile('*.xls');
handles.fileName = fullfile(file, path);
guidata(hObject,handles);
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if isfield(handles, 'fileName')
filename = get(handles.fileName);
values = xlsread(filename);
xCol = values(:,2);
yCol = values(:,3);
set(handles.axes1,'Visible','on');
plot(handles.axes1,xCol,yCol)
end
  댓글 수: 3
Avinav Kumar
Avinav Kumar 2020년 10월 12일
I am getting this error:
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)understandingthree('pushbutton2_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
Avinav Kumar
Avinav Kumar 2020년 10월 12일
Error using get
Invalid handle
Error in understandingthree>pushbutton2_Callback (line 92)
filename = get(handles.fileName);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in understandingthree (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)understandingthree('pushbutton2_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.

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

채택된 답변

Walter Roberson
Walter Roberson 2020년 10월 12일
handles.fileName = fullfile(file, path);
That creates fileName as a character vector field inside handles.
filename = get(handles.fileName);
If handles.FileName were a handle, then that would return a structure of public properties of the handle.
However, handles.FileName is not a handle, just a character vector. You just need
filename = handles.fileName;
  댓글 수: 2
Avinav Kumar
Avinav Kumar 2020년 10월 12일
Still i am getting error having changed to filename = handles.fileName;
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[file, path] = uigetfile('*.xls');
handles.fileName = fullfile(file, path);
guidata(hObject,handles);
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%filename = get(handles.import,'String');
if isfield(handles, 'fileName')
filename = handles.fileName;
values = xlsread(filename);
xCol = values(:,2);
yCol = values(:,3);
set(handles.axes1,'Visible','on');
plot(handles.axes1,xCol,yCol)
end
Walter Roberson
Walter Roberson 2020년 10월 12일
[file, path] = uigetfile('*.xls');
handles.fileName = fullfile(file, path);
The first output from uigetfile is the file name, and the second output is the directory.
The first input to fullfile should be the directory and the second input is the file name.
You are passing in the file name first and then the directory name, so you are going to be constructing a file name such as h93_b3.xlsx\Users\Aninav\Desktop\MATLAB\project7 when you wanted \Users\Aninav\Desktop\MATLAB\project7\h93_b3.xlsx

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by