我在 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 轴上绘制文本列。我可以使用 xlsread
和 plot
轻松处理数字列,但我无法管理绘制文本列。我该怎么做?
我写了下面的代码,但是我不知道对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
在您的示例中,这会产生:
如果您想跳过 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/