MATLAB Answers

0

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

Asked by Abhiruchi Rathi on 24 Oct 2019
Latest activity Answered by Akira Agata
on 25 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
.
.
.

  0 Comments

Sign in to comment.

Products

1 Answer

Answer by Akira Agata
on 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);

  0 Comments

Sign in to comment.