MATLAB Answers

0

Plot multiple y-value on a single x-value

Asked by Mohammed Hammad on 18 Mar 2019
Latest activity Commented on by Star Strider
on 23 Mar 2019
Hello,
I have array with two field (year and xx), for each year it has multiple y-value.
for exampel:
year = [2000 2001 2005 2008]
xx = [(5 10 20); (40 60); (30 20 10 50); (1)]
I am trying to plot scatter for each year (x-axis) all the coressponding values of xx (y-axis)
I was trying do it like this:
dataplot = [];
dataplot = [dataplot; year xx yy zz];
[ay,~,cy] = unique(dataplot(:,1),'rows'); % to get just the unique year
figure
scatter(year,xx,'*');
ax = gca;
ax.XTick = 1:numel(ay);
ax.XTickLabel = ay;
ax.XLim = [0 numel(ay)+1];
also I tried the solution from (Q&A) after changing it but also I didn;t get what I want.
Please find the mat file of the array in the attachements, (PS just the first two fields).
Thanks in advance

  0 Comments

Sign in to comment.

Products


Release

R2016a

1 Answer

Answer by Star Strider
on 18 Mar 2019
 Accepted Answer

Try this:
year = [2000 2001 2005 2008];
xx = {[5 10 20]; [40 60]; [30 20 10 50]; 1};
figure
hold all
for k1 = 1:numel(year)
plot(ones(1,numel(xx{k1}))*year(k1), xx{k1}, 'p')
end
hold off
It is necessary to put ‘xx’ in a cell array, since the vector lengths are different.
Experiment to get the result you want.
Plotting your actual data are even easier, since all the data are the same and each row has an associated year:
D = load('dataplot.mat');
data = D.dataplot;
figure
plot(data(:,1), data(:,2:end)', 'p')

  12 Comments

If you need to use this for every one of 300 figures, you can either copy and paste my code to each one (inconvenient), or save this function:
function yrtix(axhand)
xt = get(gca, 'XTick');
Yu = unique(fix(xt));
xtix = linspace(min(Yu), max(Yu), numel(Yu));
set(gca, 'XTick', xtix)
end
to yrtix.m in your MATLAB search path, and then do this with each figure (convenient):
D = load('data2.mat');
data = D.ALGOPARK_DATA;
figure
plot(data(:,1), data(:,2:end)', 'p')
yrtix(gca)
That worked when I tested it.
Yeah the function works perfect. BUT still the question:
(WHY MATLAB DOESN'T PLOT JUST THE DATA IN THE ARRAY WITHOUT ADDING MORE TICKS !?)
I have no idea. Use the Contact Us link in the upper right corner of this page and ask MathWorks!
I’m happy my function works, though!

Sign in to comment.