MATLAB Answers

0

Getting Pop-up menu output as Excel sheet for plotting

Asked by Varun Kumar on 16 May 2019
Latest activity Commented on by Walter Roberson
on 21 May 2019
hi everyone,
i am creating a gui which will help to compare the data from two selected sheets of excel file via plotting, excel file consist of various sheets. i am using one pusbutton for importing the excel file and two popup menu button for selection of two desired sheets of excel file and finally one axes to plot the data. my problem is that when i select a particular sheet from popup menu its not getting selected as file.

  3 Comments

Please share your code as .m file or text because it is impossible to run it via screenshot.
when i select particular sheet in popup menu for plotting i am not getting plot and getting error as shown.
error.png
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)
handles.filename = uigetfile('*.xls')
guidata(hObject, handles)
setPopupmenuString(handles.popupmenuX, eventdata, handles)
setPopupmenuString(handles.popupmenuY, eventdata, handles)
set(handles.popupmenuX, 'callback', 'sheetstoread2(''updateAxes'',gcbo,[],guidata(gcbo))')
set(handles.popupmenuY, 'callback', 'sheetstoread2(''updateAxes'',gcbo,[],guidata(gcbo))')
function setPopupmenuString(hObject, eventdata, handles)
filename = handles.filename
[~, Sheets] = xlsfinfo(filename)
set(hObject, 'string', Sheets)
function updateAxes(hObject, eventdata, handles)
filename = 'handles.filename.xls'
data1 = get(handles.popupmenuX, 'Value')
data11 = get(handles.popupmenuX, 'String')
data111=xlsread(handles.popupmenuX,' data11')
data2 = get(handles.popupmenuY, 'Value')
data22 = get(handles.popupmenuY, 'String')
data222=xlsread(handles.popupmenuY, 'data22')
a=data111(:,19);
b=data111(:,29);
c=data111(:,32);
d=data111(:,35);
A=data222(:,19);
B=data222(:,29);
C=data222(:,32);
D=data222(:,35);
plot(handles.axes1, a,b,'r',a,c,'g',a,d,'b','MarkerSize',10)
hold on
plot(handles.axes1, A,B,'k',A,C,'m',A,D,'c','MarkerSize',1)
hold off
legend('201-X','201-Y','201-Z','202-X','202-Y','202-Z')

Sign in to comment.

Tags

1 Answer

Answer by Geoff Hayes
on 19 May 2019
 Accepted Answer

Varun - I don't understand this line of code
data111=xlsread(handles.popupmenuX,' data11')
Why are you using the handle of the popupmenu (X) as the file name? In your initial code post, you are using a filename field from the handles structure. Why the change? Also, why are you putting data11 in quotes? (Again this differs from your initial code post...)

  6 Comments

Varun - from your code
index_selected = get(hObject, 'Value')
file_list=get(hObject, 'String')
selected=file_list{index_selected}
filename=handles.filename
data1=xlsread(filename(selected))
what is selected? Is it a filename (since you are extracting it from the file_list)? In that case, why do you need to do
data1=xlsread(filename(selected))
where filename is (supposedly) a list of filenames? Why can't you just do
data1=xlsread(selected)
instead?
Also, you may want to rename some of your variables to make it more clear what they represent. For example, if selected is a file name that the user has selected, why not name it selectedFilename?
ya i got it thank you so much.
i modified my code as
[~, Sheets2]=xlsfinfo(handles.filename)
set(hObject,'String',Sheets2)
index_selected = get(hObject, 'Value')
sheet_list=get(hObject, 'String')
selectedsheet=sheet_list{index_selected}
filename=handles.filename
data2=xlsread(filename, selectedsheet)
after this i wanted to plot the data using pushbutton callback with code
a=data1(:,19);
b=data1(:,29);
c=data1(:,32);
d=data1(:,35);
A=data2(:,19);
B=data2(:,29);
C=data2(:,32);
D=data2(:,35);
plot(handles.axes1, a,b,'r',a,c,'g',a,d,'b','MarkerSize',10)
hold on
plot(handles.axes1, A,B,'k',A,C,'m',A,D,'c','MarkerSize',1)
hold off
i am getting error as
Undefined function or variable 'a'.
Error in sheetstoread5>pushbutton2_Callback (line 162)
plot(handles.axes1, a,b,'r',a,c,'g',a,d,'b','MarkerSize',10)
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in sheetstoread5 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)sheetstoread5('pushbutton2_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
please help me to resolve
I suspect that your
a=data1(:,19);
is not in the same function that
plot(handles.axes1, a,b,'r',a,c,'g',a,d,'b','MarkerSize',10)
is in.
Also, your code
[~, Sheets2]=xlsfinfo(handles.filename)
set(hObject,'String',Sheets2)
index_selected = get(hObject, 'Value')
sheet_list=get(hObject, 'String')
selectedsheet=sheet_list{index_selected}
gets the names of the sheets from xlsinfo, sends it to the the popup string list, and then immediately without giving any time to draw the control or for the user to choose something, asks which value the user chose.

Sign in to comment.