Writing a function including text keep getting an error
Mostrar comentarios más antiguos
Hi,
I'm trying to write a function that plots a set of points. I wrote a script and it all worked fine, and now I'm in the process of decomposing my script into functions. Then I have taken the bit for plotting the text and tried to put it into a function, but I keep getting an error message about how I've used text.
But I don't understand what I've done wrong.
Any help would be greatly appreciated!
I've included my code, function and error below
Code which works fine:
x=[2, 2, 2, 3, 3, 3, 4, 4, 4];
y=[2, 5, 8, 2, 5, 8, 2, 5, 8];
x1= x+5;
x2= x1(1:6);
y2= y(1:6);
x3=x1(7:9);
y3=y(7:9);
i=1:5;
%define character variable
img=char('a', 'b', 'c', 'd' ,'e');
imz=char('f','g','h','i','j');
figure(i)
%set axes to show size
axis([0 10 0 12])
text(x,y,img(i))
%second rectangle with a mix
text(x2,y2, imz(i))
hold on;
text(x3,y3, 'm')
%draw rectangle
myrectangle = rectangle('Position', [1, 1 ,4, 8], 'EdgeColor', [0 0 0]);
myrectangle1 = rectangle('Position', [6, 1 ,4, 8], 'EdgeColor', [0 0 0]);
This is the function
function place_letter
i=1:5;
x=[2, 2, 2, 3, 3, 3, 4, 4, 4]; %box1
y=[2, 5, 8, 2, 5, 8, 2, 5, 8]; %box1
x1= x+5; %box 2
x2= x1(1:6); %box 2
y2= y(1:6); %box 2
x3=x1(7:9); %box 2
y3=y(7:9); %box 2
img=char('a', 'b', 'c', 'd' ,'e');
imz=char ('f','g','h','i','j');
axis([0 10 0 12]) %set axes to show size
axis on
text(x,y, img(i))
text(x2,y2,imz(i))
text(x3,y3, 'm')
These are my error messasges
*Error using text
Each string specified must have a corresponding set of coordinates
Error in place_letter (line 16)
text(x,y, img(i))
Error in Images (line 8)
place_letter*
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Annotations en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!