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

14 views (last 30 days)
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
.
.
.

Answers (1)

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

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!