2017-02-15 24 views
3

我有一個很長的字符串,我想把它作爲描述性文本添加到子圖中。如何在使用`text`繪圖時包裝字符串?

​​

我都想盡一句後增加新的行字符,使其更適合。

subplot(1,2,2); 
with_new_lines = regexprep(description, '\.', '\.\n'); 
text(0.5, 0.5, with_new_lines, 'FontSize', 14', 'FontWeight', 'Bold', ... 
    'HorizontalAlignment', 'Center', 'VerticalAlignment', 'middle') ; 

但它仍然不適合在軸內。

有沒有辦法來動態包裹字符串以滿足次要情節?

enter image description here

+3

http://weknowmemes.com/wp-content/uploads/2013/06/fire-distinguisher.png – gnovice

+1

@gnovice我愛的米姆,但我不認罪;這是一個來自羣衆的數據集[SentencesNYUv2](http://www.cs.toronto.edu/~fidler/projects/sentences3D.html)。 – Cecilia

+0

@Sardar_Usama那不符合動態標準 – Cecilia

回答

3

如何使用註釋框,按FitBoxToText財產了嗎?

description = 'This kitchen has white cabinets and two blue chairs. The upper cabinet has a black microwave. The paper towels are above the trash can. There is a black garbage can just on the left of the blue chair. On its left there is a red fire distinguisher.'; 
figure;subH=subplot(1,2,2); 
pos=get(subH,'Position'); 
annotation('textbox', pos,... 
    'String', description,... 
    'FitBoxToText','off'); 

您可以通過改變pos一號兩個元素,這(我認爲)描述了左下角,卻忘記了改變位置。

1

您可以使用textwrap功能在以下兩種方法之一:

  1. 自動換行以適合文本uicontrol內:

    hText = uicontrol('Style', 'Text', 'Position', ...(some starting position)...); 
    [wrappedText, newPosition] = textwrap(hText, {description}); 
    set(hText, 'String', wrappedText, 'Position', newPosition); 
    
  2. 裹在一個固定的數列文繪製之前用text

    wrappedText = textwrap({description}, 20); 
    text(0.5, 0.5, wrappedText, 'FontSize', 14', 'FontWeight', 'Bold', ... 
        'HorizontalAlignment', 'center', 'VerticalAlignment', 'middle'); 
    
+0

很高興知道我可以在'text'中使用'textwrap'。我瀏覽了文檔,但我專注於討論uicontrols。您的解決方案絕對優於我的解決方案,但我仍然會將Jon視爲一種動態調整圖形大小解決方案的答案。希望我能把它交給你們兩個。 – Cecilia

+0

@ Cecilia:如果經常發生調整大小,我肯定會和Jon的答案一起去。 'textwrap'最適合靜態元素,尤其是uicontrols。 – gnovice

相關問題