Search code examples
matlabplotmatlab-figureaxisxticks

How can I build custom xticklabels using numbers and strings?


I am trying to customize the Xticklabels of my bar graph to have a format of 'number (units)'

So far I have a vector:

scanrate = [2;4;6;8;10];

I want my bar graph to have an x axis of:

2mv/s 4mv/s 6mv/s 8mv/s 10mv/s

If I use xticklabels(num2str(scanrate)) the xticklabels change to the numbers in the scanrate vector. I want to put mv/s after each Xtick.


Solution

  • You can also use strcat :

    xticklabels(strcat(num2str(scanrate),' mv/s'))
    

    Please note that it works only when scanrate is a column vector.


    Fun fact :

    num2str(scanrate) + " mv/s"
    

    also works, but

    num2str(scanrate) + ' mv/s'
    

    does not