Effacer les filtres
Effacer les filtres

Import Excel and plot

2 vues (au cours des 30 derniers jours)
Ean Soo
Ean Soo le 22 Fév 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)

Réponse acceptée

Matt Tearle
Matt Tearle le 22 Fév 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 commentaire
Ean Soo
Ean Soo le 22 Fév 2011
THanks Matt, you give me a better understanding on this

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Import from MATLAB dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by