Writing data on a blank figure

I want to generate a blank figure/image and write a text and variable value using a for loop. For example, writing X:y where X is just a text (which does not change) but value of y is written from a variable and it keeps changing with each iteration. I tried using 'text' but have not got the output i want.
Thanks.

Respuestas (2)

KSSV
KSSV el 6 de Mzo. de 2018
N = 10 ;
figure
hold on
x = 0.5 ;
for i = 1:N
y = rand ;
plot(x,y,'.r')
text(x,y,num2str(i))
end

1 comentario

HT
HT el 6 de Mzo. de 2018
X is a string and y is an array of string too. For example for i=1, display "X = apple" on a blank image where apple will be y(1).

Iniciar sesión para comentar.

Walter Roberson
Walter Roberson el 6 de Mzo. de 2018

1 voto

text() does that for display purposes. But if you are aiming for an image you would have to capture the axes content with getframe() or saveas() or print() to use text()
If you need an image then the alternative is to use the Computer Vision Toolbox with its insertShape and insertText functions.

6 comentarios

HT
HT el 6 de Mzo. de 2018
Editada: HT el 6 de Mzo. de 2018
I have tried something like this:
hf = 255 * ones(300,600, 'uint8');
hold on
for i=1:3
if thisCue(i)==2
insertText(hf, [0.5 0.5], text_str(1),'FontSize',15, 'TextColor', 'black');
imshow(hf)
end
end
but it is not printing text on the figure.
Walter Roberson
Walter Roberson el 6 de Mzo. de 2018
You have to assign the result of insertText to hf.
HT
HT el 6 de Mzo. de 2018
Thanks Walter. Can I do something like: 'X=' and the display value of text_str(i) using insertText?
hf = insertText(hf, [0.5 0.5], ['X =', text_str(i)], ....)
or if text_str(i) is numeric, sprintf() up something
sprintf('X = %g', text_str(i))
HT
HT el 8 de Mzo. de 2018
I am getting this error: The length of TEXT must be equal to the number of rows in POSITION. when I do
hf = insertText(hf, [10 10],['X= ', text_str(7)],'FontSize',20,'BoxColor','w');
Walter Roberson
Walter Roberson el 8 de Mzo. de 2018
Editada: Walter Roberson el 8 de Mzo. de 2018
Is it possible that text_str is a cell array rather than a character vector or string object ? If so then you would need text_str{7} rather than text_str(7)
This presumes that if text_str is a cell, that text_str{7} only contains a single row of text.

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Preguntada:

HT
el 6 de Mzo. de 2018

Editada:

el 8 de Mzo. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by