0

I have plotted through gscatter in MATLAB. The plot of speed against year. Now there is another column vector or table in my case which contains corresponding strings of the plotted points. That table is int variable.

I want to show the corresponding text from that int variable of the plotted points on the graph. Can anyone help me?

Here is the link to my data.

Here is my code:

T = readtable('Data_Serial.xlsx');

int = T.Int;
BW = T.Band;
type = T.Type;
year = T.Year;
g = {type};

labels = cellstr(int); 

% Plot Data
f = figure;
box on;
gscatter(year,BW,g,'rkgb','o*',8,'on','Year','Speed')
hold on;
text(year(:,1), BW(:,2), labels, 'VerticalAlignment','bottom', ...
                         'HorizontalAlignment','right')

It shows following error:

>> Data_Serial
Index exceeds matrix dimensions.

Error in Data_Serial (line 16)
text(year(:,1), BW(:,2), labels, 'VerticalAlignment','bottom', ...

enter image description here

aguntuk
  • 127
  • 9
  • 1
    Possible duplicate of [Labeling points in order in a plot](https://stackoverflow.com/questions/4140312/labeling-points-in-order-in-a-plot) – gehbiszumeis Sep 19 '18 at 11:02
  • @gehbiszumeis thanks for your answer. That example I saw. In that example they converted row numbers to string using `labels = cellstr( num2str([1:10]') );`. In my case, I have string table stored in `int`. So I put as `labels = cellstr(int);` i am using `scatter` function which enables me to plot according to another variable. But that label still doesn't work. I have updated the code in my question. – aguntuk Sep 19 '18 at 12:27

1 Answers1

0

You should start indexing from 1 in BW.

This should display the plot with text labels

text(year(:,1), BW(:,1), labels, 'VerticalAlignment','bottom', ...
                         'HorizontalAlignment','right')
Marouen
  • 907
  • 3
  • 13
  • 36