excel - 如何使用 Excel 文件中的文本数据列作为 x 轴进行绘图?

标签 excel matlab text plot

我在 Matlab 中导入了一个包含两列的 Excel 文件,一列为数值,另一列为文本值。前 5 行如下所示:

ABF-E 0.34

HJK-D -0.54

GHKL-I 1.34

FPLO-5 2.3

KKJLL-T 0.98

我需要在 Y 轴上绘制数字列,在 X 轴上绘制文本列。我可以使用 xlsreadplot 轻松处理数字列,但我无法管理绘制文本列。我该怎么做?

我写了下面的代码,但是我不知道对x轴做什么:

filename = 'MyData.xlsx';

Sheet = 2

xlRange = 'B1:B60';

Yaxis = xlsread (filename,Sheet,xlRange);

Xaxis = ????????;

plot(xAxis,Yaxis)

如果有人能帮助我,我将不胜感激。

最佳答案

设置xticklabel property轴的 xAxis 文本。你可以沿着这些路线走:

[~,xAxis] = xlsread(filename,Sheet,'A1:A60'); %// read in text data. Use second output 
yAxis = xlsread(filename,Sheet,'B1:B60'); %// read in numeric data
plot(yAxis) %// This uses 1, 2, 3... as x axis values
set(gca,'xtick',1:numel(xAxis)) %// set ticks
set(gca,'xticklabel',xAxis)  %// set ticklabels with your xAxis
xlim([0 numel(xAxis)+1]) %// adjust x axis span

在您的示例中,这会产生:

enter image description here

如果您想跳过 x 轴上的一些标签以使其不那么困惑:定义所需的 tickStep 然后在设置刻度和刻度标签时使用它(在两个 集合中 行):

tickStep = 6; 
[~,xAxis] = xlsread(filename,Sheet,'A1:A60'); %// read in text data. Use second output 
yAxis = xlsread(filename,Sheet,'B1:B60'); %// read in numeric data
plot(yAxis) %// This uses 1, 2, 3... as x axis values
set(gca,'xtick',1:tickStep:numel(xAxis)) %// set ticks
set(gca,'xticklabel',xAxis(1:tickStep:numel(xAxis)))  %// set ticklabels with your xAxis
xlim([0 numel(xAxis)+1]) %// adjust x axis span

关于excel - 如何使用 Excel 文件中的文本数据列作为 x 轴进行绘图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20434670/

相关文章:

excel - 错误处理-在哪里输入 'Exit Sub'?

matlab - 查找两个像素之间的距离的最简单方法是什么?

html - 输入类型 "number"不会调整大小

php - 如何在 Twig 中显示 HTML,同时仍然修剪显示内容的长度

php - 插入html/文本到mysql

html - 如何使用 VBA 从网页中获取产品标题?

excel:mid:找不到项目或库

vba - 逗号分隔单元格到行,但保留周围列中的数据

matlab - 使用matlab访问矩阵的最后几行

string - 在matlab中gui的文本框中设置单词 'remove'