How to use string as data for plotting in Matlab?

后端 未结 4 1072
轮回少年
轮回少年 2020-12-09 04:42

I want to use a words like, let\'s say, \'A\', \'B\' and \'C\' on X-axis to show their corresponding properties on Y-axis. How can I write these strings on X-axis instead of

相关标签:
4条回答
  • 2020-12-09 05:27

    How to use CHARACTER Values instead of Numerical values in X axis. to label x as T1 T2 T3 T4 just use this : set(gca,'XTick',1:4,'XTickLabel',{'T1', 'T2', 'T3', 'T4'},'FontSize',15)

    this command can be used after the plot command followed by the xlabel and ylabel , legand commands. you can also adjust the font size.

    Practical Example:

        %% 50% Day 
        T1wSI=[54.17 115];
        T2wSI=[53.5 112];
        T3wSI=[52.2 110];
        T4wSI=[51.2 108];
    
        T1oSI=[50.25 94];
        T2oSI=[49.18 92];
        T3oSI=[48.2 90];
        T4oSI=[46.1 84];
    
        table1=[T1wSI;T2wSI;T3wSI;T4wSI;T1oSI;T2oSI;T3oSI;T4oSI ];
        season2012=table1(:,1);
        season2013=table1(:,2);
        Tr1=[1 2 3 4];
    
    Treatment1 =['T1wSI' 'T2wSI' 'T3wSI' 'T4wSI' 'T1oSI' 'T2oSI' 'T3oSI' 'T4oSI'];
        %Tre1=['T1' 'T2' 'T3' 'T4'];
        %set(gca,'FontSize',14)
        figure(1)
        set(gca,'XTick',1:4,'XTickLabel',{'T1', 'T2', 'T3', 'T4'},'FontSize',14)
        plot(Tr1,table1(1:4,1),'--bs','LineWidth',3);% 2012
        hold on;
        plot(Tr1,table1(1:4,2),'-go','LineWidth',3);% 2013
        plot(Tr1,table1(5:8,1),'--r*','LineWidth',3); % 2012
        plot(Tr1,table1(5:8,2),'-m^','LineWidth',3);% 2013
        set(gca,'XTick',1:4,'XTickLabel',{'T1', 'T2', 'T3', 'T4'},'FontSize',15)
        xlim=[1 5];
        xlabel('Treatments')
        ylabel('Days to 50 % Flowering')
        legend('With -Season 2012','Without -Season 2013','With -Season 2012','Without - Season 2013','Location','NorthEast');
    
    0 讨论(0)
  • 2020-12-09 05:29

    Use 'XTick' and 'XTickLabel' properties of the axes handle.
    Here's a simple example:

    x = 1:5;
    y = rand(size(x));
    plot(x, y, 'b')
    set(gca, 'XTick',1:5, 'XTickLabel',{'A' 'B' 'C' 'D' 'E'})
    

    alt text

    0 讨论(0)
  • 2020-12-09 05:37

    You can also do this using the GUI.

    1) Click on the figure axes to to open the Axes Property Editor.

    2) Click on the "More properties" button on the right side of the window. This will open the inspector window of the axes.

    3) Click on the small button next to "XTickLabel" property to open the dialogue box as shown below. enter image description here

    4) Enter the labels you want and click on "OK".

    0 讨论(0)
  • 2020-12-09 05:41

    Set yourself up a cell with your letters (mine's called labels), then use the XTick property to set the same amount of ticks on the x axis as your label number. Finally, the XTickLabel property will write your labels to the x axis.

    x = yourXdata;
    y = yourYdata;
    labels = {'A' 'B' 'C'};
    plot(x, y);
    set(gca, 'XTick', 1:3, 'XTickLabel', labels);
    
    0 讨论(0)
提交回复
热议问题