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

69 vues (au cours des 30 derniers jours)
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?

Réponse acceptée

Adam Danz
Adam Danz le 15 Sep 2020
Modifié(e) : Adam Danz le 15 Sep 2020
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 commentaires
Ann St
Ann St le 15 Sep 2020
Modifié(e) : Ann St le 15 Sep 2020
@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.
Adam Danz
Adam Danz le 15 Sep 2020
Yep, I'll fix that.
Glad I could help!

Connectez-vous pour commenter.

Plus de réponses (1)

Xavier
Xavier le 15 Sep 2020
Try using
set(gca, 'XTickLabel',stringArray)
instead, you don't need a cell array to have labels
  3 commentaires
Adam Danz
Adam Danz le 15 Sep 2020
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)
Ann St
Ann St le 15 Sep 2020
@Adam Danz. Thank you for that--very nice of you. I implemented your suggestion but still get repeated labels.

Connectez-vous pour commenter.

Produits


Version

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by