Plotting an array of string as X-axis and an array of numbers as y-axis?

15 vues (au cours des 30 derniers jours)
Abhiruchi Rathi
Abhiruchi Rathi le 24 Oct 2019
I have a column of strings in the form of quarters given by- 1960:1, 1960:2, 1960:3, 1960:4, 1961:1, 1961:2...1997:2 and a column of numbers. How do I plot the two on the same graph such that the strings correspond to the numbers and the strings feature on the x-axis?
1960:1 0.434216
1960:2 0.389105
1960:3 0.430663
1960:4 0.343053
1961:1 0.213675
1961:2 0.255864
.
.
.

Réponses (1)

Akira Agata
Akira Agata le 25 Oct 2019
Assuming your data was stored in the attached format, I think there should be at least following 2 solutions:
% Read data
data = readcell('data.txt');
% [Solution1] Use datetime vector
Time = datetime(data(:,1),'Format','yyyy:Q');
figure
plot(Time,cell2mat(data(:,2)))
% [Solution2] Replace XTickLabel
figure
plot(cell2mat(data(:,2)))
ax = gca;
ax.XTick = 1:size(data,1);
ax.XTickLabel = data(:,1);

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by