Clear Filters
Clear Filters

Import Excel and plot

2 views (last 30 days)
Ean Soo
Ean Soo on 22 Feb 2011
Hie everyone, i face some problems that i do not actually know where is the problem is. Let's say i have an excel file with two columns. First is 'frequency' and second column is 'data'. When i import the excel file into my GUI, i put two popupmenu two choose for y-axis and x-axis. The problems is, the popupmenu only manage to shows the frequency, for both axis, but not the data. However, when i click the blank space below the frequency which is suppose to be the data words, it manage to plot the graph. So i want to ask, how can i make the data words appear in the popupmenu. Below is the code for my GUI. Regards, Ean
handles.fileName=uigetfile('*.xls') guidata(hObject,handles) setPopupmenuString(handles.popupmenuX,eventdata,handles) setPopupmenuString(handles.popupmenuY,eventdata,handles) set(handles.popupmenuX,'callback','maingui(''updateAxes'',gcbo,[],guidata(gcbo))') set(handles.popupmenuY,'callback','maingui(''updateAxes'',gcbo,[],guidata(gcbo))') function setPopupmenuString(hObject,eventdata,handles)
fileName = handles.fileName;
[numbers,colNames] = xlsread(fileName);
set(hObject,'string',colNames);
function [x,y] = readExcelColumns(fileName,xColNum,yColNum)
a = xlsread(fileName);
x = a(:,xColNum); y = a(:,yColNum);
function updateAxes(hObject,eventdata,handles)
xColNum = get(handles.popupmenuX,'value')
yColNum = get(handles.popupmenuY,'value')
fileName = handles.fileName;
[x,y] = readExcelColumns(fileName,xColNum,yColNum)
plot(handles.axes1,x,y)

Accepted Answer

Matt Tearle
Matt Tearle on 22 Feb 2011
Run the line [numbers,colNames] = xlsread(fileName); from the Command Window (with the appropriate fileName filled in, obviously) and take a look at colNames. It is almost certainly going to be a whole cell array of text entries. This means that the actual column header strings you want are colNames{1,1} and colNames{1,2}. Because MATLAB is column-major, and allows linear indexing, I suspect that set(hObject,'string',colNames); is essentially getting colNames{1,1} and colNames{2,1} (ie going down the first column). But {2,1} is going to be blank.
So, short answer: replace
set(hObject,'string',colNames);
with
set(hObject,'string',colNames(1,1:2));
  1 Comment
Ean Soo
Ean Soo on 22 Feb 2011
THanks Matt, you give me a better understanding on this

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import from MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!