How do I plot data points with x-axis tick labels that are strings?

hello there, i have 15 (x) points named x1 to x15; and corresponding to this i have 15 (y) points named from y1 to y15. how can i plot them ?

4 comentarios

Don't create variable names like this in the first place. As you can tell, it becomes hard to work with them downstream in your code. Instead, rewrite your code to produce a single variable named x that has all of the 'x' values, and another variable called y for the 'y' values. Then it is simple:
plot(x,y);
i did it but it keeps giving this message: Error using plot Invalid first data argument.
the y values i have are numerical, the x values are strings; example: x1= class1, x2=class2 etc.. y1= 0.05, y2=15 etc
how can you do it with plot(data, ‘.’, (signalNoise,signalTime)
I have the suspicion that you might be attempting to plot variables in a table() object. No currently released version of MATLAB supports that directly; you would need to instead
plot(data.signalTime, data.signalNoise, '.')

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 29 de Ag. de 2023
Editada: MathWorks Support Team el 29 de Ag. de 2023
The easiest way to specify the tick labels as strings is to use the xticklabels function. For example, plot five points. Change the ticks to only five values by calling the xticks function. Then change the x-axis tick labels to “x1”, “x2”, “x3”, “x4”, and “x5”. 
plot(1:5) 
xticks(1:5) 
xticklabels(["x1", "x2", "x3", "x4", "x5"]) 
Another way to combine a character with a sequence of numbers is to use the xtickformat function. 
plot(1:5) 
xticks(1:5) 
xtickformat("x %g ") 
Alternatively, you can plot categorical values. Specify the second argument when you call the “categorical” function to ensure that the data is in the expected order. 
x = categorical(["Red", "Yellow", "Blue"],["Red", "Yellow", "Blue"]);
plot(x,1:3)

11 comentarios

i did it but it keeps giving this message: Error using plot Invalid first data argument.
the y values i have are numerical, the x values are strings; example: x1= class1 x2=class2 etc.. y1= 0.05 y2=15 etc
You cannot use strings as x coordinates in plot(). You can use integers instead and set the axis xtick positions to the integers and set the xticklabels to the strings. You might also be able to plot using a categorical() as the x axis.
thank you very much , this answer makes big sense, can you please provide me with the suitable code for this if you please
x = {x1, x2, x3, x4, x5, x6, x7, x8, x9};
y = [y1, y2, y3, y4, y5, y6, y7, y8, y9];
plot(y);
set(gca, 'XTick', 1:length(y), 'XTickLabel', x)
thank you very much Sir
x = (x1 : x15);
y = (y1 ; y15);
plot(x,y)
No, that will not work. x1:x15 takes the value in x1 and constructs a vector of consecutive numbers until the value of x15. For example if x1 was 2.7 and x15 were 8.3 then x1:x15 would be [2.7, 3.7, 4.7, 5.7, 6.7, 7.7]
you need to just change onething in your program
write plot function like this
Z = plot(x,y)
set(Z,'marker','.','markersize',8)
like this you can plot the graph into dots
plot(x, y, '.', 'markersize', 8)
What if we have large number of data points..say 70?
How can I then obtain a plot in matlab?
Please read http://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval for information about why we strongly recommend against creating variable names dynamically.

Iniciar sesión para comentar.

Más respuestas (1)

ejjada
ejjada el 16 de Mzo. de 2024
plz anyone answer me
how to read my hole x axis data as x1,x2,x3,x4,...

Etiquetas

Aún no se han introducido etiquetas.

Preguntada:

el 18 de Sept. de 2017

Comentada:

el 16 de Mzo. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by