How can I label the x and y axis in this plot using the name of the Excel columns?

2 views (last 30 days)
Hello all, I have a function that takes an Excel file as an input, and outputs a graph using the 2nd column of numbers as the x values and the 3rd column of numbers as the y values. This is my script:
function plotData(filename, markers)
a=xlsread(filename);
hold on;
title(filename);
x=a(:,1);
y=a(:,2);
if markers==true
plot(x,y,'-o')
elseif markers==false
plot(x,y)
end
The problem I am having is naming the x axis and y axis. I need help fixing the script so that it takes the first entry from each column as a string, and names the x and y axis with this. Here is what the Excel file looks like:
A B
1
2 time (sec)height (meters)
3 0 200
4 1 195
5 2 180
6 3 155
7 4 120
8 5 75
9 6 20
The output should be a plot of the data (which works fine) with the x axis labeled 'time(sec)' and the y axis labeled 'height (meters)'. I know how to label them by 'xlabel' and 'ylabel' but I need the script to take the names from the Excel file as a string and label them instead of me manually entering the names into my MATLAB script.

Accepted Answer

dpb
dpb on 26 Nov 2017
Edited: dpb on 26 Nov 2017
a=xlsread(filename); % only returns the numeric data from the spreadsheet.
Look at
doc xlsread
and use the alternate return values to also retrieve the text.
[a,t]=xlsread(...
You'll have to look at the content of the cell to ascertain whether the variable names and units are in one cell each or have been wrapped horizontally so there are only two cells and deal with the returned content appropriately to build the label strings.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!