How can I create a plot with column headings along the X-axis?

Hi I need to plot a matrix such that every column heading is listed along the x-axis and every cell in that column is plotted along the Y-axis? I have attached a picture of the same. I am a newbie to MATLAB. Could someone please help? Thanks in advance Deepa

 Akzeptierte Antwort

Try this:
M = randi(9, 3); % Create Matrix
x = 1:size(M,2); % Column numbers
labelstr = sprintf('Column %d\n', x); % Create Labels
labelcel = regexp(labelstr, '\n', 'split'); % Split & Create Cell Array
figure(1)
plot(x, M', '.b', 'MarkerSize',20)
set(gca, 'XTickLabel',[])
set(gca, 'XTick',x, 'XTickLabel',labelcel(1:end-1), 'XTickLabelRotation',90)
axis([0 4 0 10])

4 Kommentare

Thank you very much, Star Strider. Could I ask for more??
I already have the column headers in another 1x4 matrix. CellA1 contains heading of column 1 . CellA2 contains heading of column 2 and so on. Is there a way to label with these headings instead of 'column 1', 'column 2' etc... along the x-axis?
Thank you very much for helping out.
Deepa
As always, my pleasure.
If your column headers are in a cell array (here I call it ‘YourCellArray’), the line that writes the x-axis labels changes to:
set(gca, 'XTick',x, 'XTickLabel',YourCellArray, 'XTickLabelRotation',90)
That should work.
Thank you very much. It is working perfectly now.
As always, my pleasure.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by