Effacer les filtres
Effacer les filtres

Writing a function including text keep getting an error

5 vues (au cours des 30 derniers jours)
Alice le 12 Nov 2014
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);
%define character variable
img=char('a', 'b', 'c', 'd' ,'e');
%set axes to show size
axis([0 10 0 12])
%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
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(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)

Réponse acceptée

Geoff Hayes
Geoff Hayes le 12 Nov 2014
Alice - I think that you are missing a for loop from each block of your code (this sort of seems true due to the indenting of your above code). The first block fails with the error
Error using figure
Single input must be an existing figure handle or a scalar integer from 1 to 2147483646
because of how the local variable i is initialized and then used
% other code
You should have your loop doing something like
for k=1:5
% etc.
I replaced the i with k as the index variable since MATLAB uses i and j as representations of the imaginary number.
In your function place_letter, you are missing that same for loop and the call to figure, so the error (from above which you observed) occurs because i is an array of five elements, and you are using this local variable at
text(x,y, img(i))
where img(1:5) is a column vector of 5 strings whereas you seem to just want a single character/letter. Try using a for loop here too so that a single character is placed at each iteration of the loop on different figures.
However, if you are trying to place a string of characters and not a single character, say 'abcdef', then transpose the column vector to a row vector using the apostrophe. i.e.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by