How to plot using a text data column from an Excel file as x axis?

excel, matlab, plot, text

Solution

Set the `xticklabel` property of the axis with your `xAxis` text. You can go along these lines:

[~,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

In your example, this yields:

If you want to skip some labels on the x axis to make it less cluttered: define the desired `tickStep` and then use it when setting the ticks and the ticklabels (in the two `set` lines):

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

Problem

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 cannot 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.

Original source