Re-hash the intrinsic Matlab plot function to accept a string as a y input?

1 view (last 30 days)
I am using the matlab 'plot' function which uses x, y as inputs. The x axis is time (which is a number in seconds and is easy), however the y axis is actually a number/string combination, for example, E312, E313. The plot function breaks since it is only expecting a numeric value, however I want the ylabel (for that corresponding point) to display 'E312' (in this example); how is that possibe?
  2 Comments
Walter Roberson
Walter Roberson on 17 Apr 2019
If I have (say) 500 pixels available for y axes, then how far up should 'E312' appear? How far up should 'E313' appear? Is 'E1000' above or below 'E2' ?
nas illmatic
nas illmatic on 17 Apr 2019
Edited: nas illmatic on 17 Apr 2019
Yes they should be in chronical order - for example if 0 is where the x-axis and y-axis meet, then E312 should be in the next one up, then E313, then E314, ... , E1000. At first I was going to use the 'ylabel' but then I had more than one x,y pair - so now I need the string to appear on the y-axis in chronical order. The 'plot' function only takes in a numerical (and would imagine the str2num removes all letters...
As far as the spacing (i.e, ticks), they should be divided by the number of x,y pairs, for example if there are 10 x,y pairs (0.1, E313) ... then the first x,y pair should be evenly spaced within the 'figure' window (up one tick) over 10 ticks. As far as the 'pixels' that is not as important, simply evenly spacing them based on the total number of points is sufficient in this case. The more important aspect the x-axs time. The y axis (i.e., E313) designates a string labeling that represents the series of points. They would be chronical and evenly spaced but the 'pixel' spacing is not important. The reader of the plot will be concerned of the time spacing between points of the series and the label designation (on the y-axis) which is a string (and hence breakes when inputted as the y-input on the 'plot' function)...
I want the reader to be able to see the series of points spaced in time (on the x-axis) and look at the y-axis as a designator of what that series of points represent (which is a 'string' that is read in from the data (from a variable - actually from a cell in a struct). I would imagine this has been done before as it's a good representation of the data but could not find it...

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 17 Apr 2019
You could turn your text data into a categorical array then plot with that categorical data as the Y input.
colors = categorical({'r' 'b' 'g', 'g' 'r' 'b', 'b' 'r' 'g'}, ...
{'r' 'g' 'b'},{'red' 'green' 'blue'});
plot(1:9, colors)
Alternately, if you want to plot numeric data and just have the labels on the axes change, use the yticklabels function.
  1 Comment
nas illmatic
nas illmatic on 17 Apr 2019
Edited: nas illmatic on 17 Apr 2019
The example has 'y=0' but would imagine could replace that w/ a string variable and use the yticks to evenly space for each. Maybe something like this:
for I=1:size(s.name,1)
hold on; figure; %so that it holds during the loop
plot(time,I) %plot time which is the most important for each s.name
yticklabels({s.name(I)}) %overwride ylabel w/ name from cell of struct
yticks([0:I]) %evenly space
end
You think this should do the trick?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!