Import Excel and plot

2 次查看(过去 30 天)
Ean Soo
Ean Soo 2011-2-22
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)

采纳的回答

Matt Tearle
Matt Tearle 2011-2-22
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 个评论
Ean Soo
Ean Soo 2011-2-22
THanks Matt, you give me a better understanding on this

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Import from MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by