How can I plot data from a text file--an array of numbers versus an array of strings?

I want to plot data from a text file, where x is an array of categories (strings) and y is an array of numbers. I have followed examples online, but so far nothing is working. If anyone could please point me in the right direction, I would be very grateful.
For example, after I have read in my array of strings and print it out, I get:
'Dry'
'Wet'
'Saturated'
'Wet'
'Wet'
'Dry'
The array of numbers is the same size as the array of strings (i.e., there's a value to match each string entry):
1
2
3
4
5
6
1. I have tried (among other things):
labels = {stringArray};
plot(numberArray, 'o', 'MarkerSize', 5)
set(gca, 'XTickLabel',labels)
But get this error:
Error using matlab.graphics.axis.Axes/set
While setting property 'XTickLabel' of class 'Axes':
Cell array can only contain character vectors or numbers.
Error in metadata_plots_vi_text (line 38)
set(gca, 'XTickLabel',labels)
2. I also tried writing this for "labels"
labels = [WRB_ISRIC];
This gives me a plot where the X axis has "Wet Wet Wet Dry Saturated" instead of "Wet Dry Saturated."
Does anyone know an alternative?

 Accepted Answer

Here's a variety of ways to use the strings as axis ticks.
After a second look, you're probably looking for the last method.
stringArray = {
'Dry'
'Wet'
'Saturated'
'Wet'
'Wet'
'Dry'};
numberArray = [
1
2
3
4
5
6];
clf
s(1) = subplot(4,1,1);
plot(numberArray, categorical(stringArray), 'o-');
title('plot()')
s(2) = subplot(4,1,2);
stem(numberArray, categorical(stringArray), 'o-');
title('stem()')
s(3) = subplot(4,1,3);
[n,cats]= histcounts(categorical(stringArray));
bar(categorical(cats),n);
title('bar()')
s(4) = subplot(4,1,4);
plot(numberArray, 'o', 'MarkerSize', 5)
set(s(4), 'XTick', 1:numel(numberArray), 'XTickLabel', stringArray)
title('XTick & XTickLabel')
s(5) = subplot(5,1,5);
plot(categorical(stringArray), numberArray, 'o', 'MarkerSize', 5)
title('plot(categorical, y)')
set(s(1:2),'XTick',1:6,'XLim',[0,7])
For earlier releases of Matlab that did not support automatic assignment of categorical tick labels,
cats = categorical(stringArray);
catsUnq = categories(cats);
plot(categorical(stringArray), numberArray, 'o', 'MarkerSize', 5)
set(gca, 'XTick', 1:numel(catsUnq), 'XTickLabel', catsUnq)

13 Comments

@Adam Danz, thank you, that is getting me close. However, I have multiple "Wet" labels on the x axis instead of just one. Do you have an thoughts on why this might be happening? Thank you.
I've updated my answer to add more examples. Is your goal the 4th subplot?
@Adam Danz, thank you, very kind. My goal is almost the fourth subplot. I want only one label for each category. For example, you (and I--this is where I am now) have:
Dry Wet Saturated Wet Wet
I want only:
Dry Wet Saturated
(just one of each category). Is that possible?
Ok, I've updated my answer again.
If you want to reorder the categories, you'll have to apply
@Ann St is the 5th subplot above what you're looking for?
If so, have you applied it to your data?
plot(categorical(stringArray), numberArray, 'o', 'MarkerSize', 5)
@Adam, thanks a lot. Yes, I'm working on your s(5). It is what I want. I can't figure out how to make it work for individual figures, but it's fine because I can make my figures into subplots to get started.
If you have time (no pressure), would you mind telling me what s(1:2) refers to? I'm getting errors that my indexes exceed my matrix dimensions.
I can see that I'm getting closer though--I'm not getting labels, but I am getting numbers on the X axis that seem to correspond to labels.
"would you mind telling me what s(1:2) refers to?"
"s" is a vector of subplot handles formed by
s(n) = subplot(___)
I only want to apply the x-ticks to the first two subplots so I'm indexing "s" with s(1:2). If you want example #5, there's no need to do that.
"I'm getting errors that my indexes exceed my matrix dimensions."
I don't know what line is producing that so I cannot make a recommendation.
"I'm not getting labels, but I am getting numbers on the X axis"
Then you're not applying the categories to the x-data. Look at the variable values in my demo and compare them to yours.
@Adam Danz, thank you very, very much indeed. I never would have figured this out if not for your help. I have what I need. I used:
set(gca,'XTick',1:3,'XLim',[1,3],'XTickLabel', labels)
where "labels" is my string array. I used it with your plot method above:
plot(categorical(stringArray), numberArray, 'o', 'MarkerSize', 5)
Really appreciate it.
That's not the right approach, though. If you're calling the set(gca, ...) line first, then the second line is overriding it, so the set-line does nothing. The purpose of using categories is that it assigns the xlabels for you. You should remove the first line in your comment above.
Run this snippet to see the result.
stringArray = {
'Dry'
'Wet'
'Saturated'
'Wet'
'Wet'
'Dry'};
numberArray = [
1
2
3
4
5
6];
plot(categorical(stringArray), numberArray, 'o', 'MarkerSize', 5)
@Adam Danz, thank you. I did as you suggested (only running the plot command). When I do that, I end up with numbers on the X axis (as shown in the figure attached), not strings. Another problem is that I can't tell which categories the numbers belong to.
The only way I can get labels is to use the "set" command I posted in my last reply (but I still don't know which order they should be in--which numbers correspond to particular labels).
I see. You're using r2016a. I just tested this on 16a and apparently the automatic assignment of categorical names was not supported at that time.
Here's a safer way of doing that,
cats = categorical(stringArray);
catsUnq = categories(cats);
plot(categorical(stringArray), numberArray, 'o', 'MarkerSize', 5)
set(gca, 'XTick', 1:numel(catsUnq), 'XTickLabel', catsUnq)
I'll add this to my answer so it's more complete.
@Adam Danz, thanks a lot. I assume the "x" is on the "set" line is a typo because if I leave that out, it works!! Again, thank you. Really grateful.

Sign in to comment.

More Answers (1)

Try using
set(gca, 'XTickLabel',stringArray)
instead, you don't need a cell array to have labels

3 Comments

@Xavier, thank you very much for your help. I tried adding your suggestion.
I still have the problem that I end up with repeated labels on the X-axis. So instead of "Wet Dry Saturated," I get "Wet Wet Wet Dry Dry..."
Also, I have to use your suggestion with Adam's (above) because if I just use
plot( stringArray, numberArray)
, I get:
Error using plot
Invalid first data argument
Yes, Xavier's correct, if the goal is to set the x-tick labels.
Whenever setting XTickLabel, also set XTick. Otherwise, you risk a mismatch between the number of ticks and labels and Matlab will either wrap the labels or skip the extras by default.
set(gca, 'XTick', 1:numel(stringArray), 'XTickLabel',stringArray)
@Adam Danz. Thank you for that--very nice of you. I implemented your suggestion but still get repeated labels.

Sign in to comment.

Categories

Products

Release

R2016a

Asked:

on 15 Sep 2020

Edited:

on 15 Sep 2020

Community Treasure Hunt

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

Start Hunting!