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

13 次查看(过去 30 天)
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
.
.
.

回答(1 个)

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

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by