matlabmatlab-figure

Adding text at the end of an annotated line in matlab


I need to add text to the free body diagrams that I need to model for my task but the way my professors expect me to do it is not working so I was wondering how I can do it

This is the code I have written for my task

%work assignment 2
annotation("rectangle",[.1 .2 .5 .4]);
annotation("line",[.6 .8],[.4 .4]);
annotation("textarrow",[.6 .8],[.4 .55]);
'string'; 
('F=mg');
annotation("textarrow",[.35 .35],[.45 .8]);
annotation("textarrow",[.35 .35],[.4 .1]);

now I need to add labels to the end of the arrows and according to the task pdf I was supposed to use string to add the text but that did seem to do anything so I was wondering how I could. I am also not very good at matlab so I would appreciate if you guys could tell me where to change the code.

This is the output, the f=mg in the string bit is supposed to go on the slanted line and the other 2 arrows will also need text:

output figure


Solution

  • This is not valid MATLAB syntax, whilst it won't produce an error the 2nd and 3rd lines are basically doing nothing:

    annotation("textarrow",[.6 .8],[.4 .55]);
    'string'; 
    ('F=mg');
    

    The String name-value pair should be within the brackets of the call to annotation, because they are inputs to that function.

    annotation("textarrow",[.6 .8],[.4 .55], 'string', 'F=mg');
    

    If you want to split it over multiple lines you need ... for line continuation

    annotation("textarrow",[.6 .8],[.4 .55], ...
        'string', 'F=mg');
    

    Reference for the documentation: https://uk.mathworks.com/help/matlab/ref/matlab.graphics.shape.textarrow-properties.html

    plot

    If you want text somewhere other than the start of the arrow, you could just add a 2nd annotation which is a textbox with the same coordinate system as the arrows, or a text object with coordinates matching your axes.