How to plot a text data column from an excel file in MATLAB?

조회 수: 6 (최근 30일)
Sarah
Sarah 2013년 12월 6일
댓글: Sarah 2013년 12월 7일
Hello, I have imported in Matlab an excel file that contains two columns, one with numeric values, and the other one with text values. The first 5 rows can be seen below:
ABF-E 0.34
HJK-D -0.54
GHKL-I 1.34
FPLO-5 2.3
KKJLL-T 0.98
I need to plot the numeric column on the Y axis and the text column on the X axis. I can easily work with the numeric column using xlsread and plot, but I can not manage plotting the text column. How could I do it?.
I have written the following code, but I don't know what to do for the x Axis:
filename = 'MyData.xlsx';
Sheet = 2
xlRange = 'B1:B60';
Yaxis = xlsread (filename,Sheet,xlRange);
Xaxis = ????????;
plot(xAxis,Yaxis)
I would be very grateful if somebody could help me.
  댓글 수: 2
Image Analyst
Image Analyst 2013년 12월 6일
What does it mean to "plot" a column of text???
Sarah
Sarah 2013년 12월 6일
I'm trying to build a line graph, where the values of the first column of my excel file, which contains text values, should represent the X axis of my graph. And the second column of my excel file, should represent the Y axis. I can easily create graphs when the values of the excel file are of numeric values, but I can't manage plotting text values against numeric values.

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

답변 (2개)

sixwwwwww
sixwwwwww 2013년 12월 6일
you can do it as follows:
[num, txt, raw] = xlsread('filename.xlsx', 1);
plot(num)
set(gca,'Xtick',1:numel(num),'XTickLabel',txt)
The last line of the code set the labels for data given in first column of your excel sheet
  댓글 수: 4
Sarah
Sarah 2013년 12월 6일
Hello sixwwwwww, thanks for your answer. That works for the Xaxis, but where in the code could I put my Yaxis variable so that the Xaxis values are plotted against the Yaxis values?.
sixwwwwww
sixwwwwww 2013년 12월 6일
here is the code for plotting both x and y values on x-axis and y-axis respectively:
[num, txt, raw] = xlsread('filename.xlsx', 1);
plot(num)
num = sort(num);
for i = 1:numel(num)
NumCell(i) = {num2str(num(i))};
end
set(gca,'Xtick',1:numel(num),'XTickLabel',txt, 'Ytick', num,'YTickLabel',NumCell)

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


Azzi Abdelmalek
Azzi Abdelmalek 2013년 12월 6일
Thre is no x-axis data in your Excell file
Yaxis = xlsread (filename,Sheet);
plot(Yaxis)
  댓글 수: 5
Azzi Abdelmalek
Azzi Abdelmalek 2013년 12월 6일
Where will you put 60 worsds like ABF-E in X-axis?
Sarah
Sarah 2013년 12월 7일
yes, I have attached an image of what I'm trying to manage. I have done it in excel but I'm trying to do the same in Matlab. In the image, the graph is designed with just 5 text values on the X-axis, but I have 60 like that in my excel file.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by