但是要把1-6个数字分别匹配到h1-h6个箭头上,上面的代码set(num_text, 'Parent', h1)只有h1
时间: 2024-03-01 09:54:33 浏览: 25
对于每个箭头,您需要为其创建一个变量来存储其数字的文本对象,并将该文本对象作为箭头的子元素。下面是一个修改后的示例代码:
```matlab
% 创建箭头
h1 = annotation('arrow',[0.25 0.35],[0.85 0.85]);
h2 = annotation('arrow',[0.5 0.5],[0.85 0.6]);
h3 = annotation('arrow',[0.65 0.75],[0.85 0.85]);
h4 = annotation('arrow',[0.5 0.5],[0.85 0.4]);
h5 = annotation('arrow',[0.15 0.25],[0.85 0.85]);
h6 = annotation('arrow',[0.35 0.45],[0.85 0.6]);
% 设置箭头属性
set(h1, 'HeadWidth', 15, 'HeadLength', 15);
set(h2, 'HeadWidth', 15, 'HeadLength', 15);
set(h3, 'HeadWidth', 15, 'HeadLength', 15);
set(h4, 'HeadWidth', 15, 'HeadLength', 15);
set(h5, 'HeadWidth', 15, 'HeadLength', 15);
set(h6, 'HeadWidth', 15, 'HeadLength', 15);
% 添加数字
num_matrix = [0.8, 0.4; 0.6, -0.2; 0.9, -0.6];
num_texts = cell(6,1); % 存储数字的文本对象
for j = 1 :2
for i = 1:3
num = num_matrix(i,j);
pos_x = 1.50 + (j-1)*x_spacing + box_width/2;
pos_y =2.9 - (i-1)*0.9;
num_text = text(pos_x,pos_y,num2str(num),'HorizontalAlignment','center','FontSize',20,'BackgroundColor', 'white');
% 将数字添加为箭头的子元素
if i == 1 && j == 1
set(num_text, 'Parent', h1);
num_texts{1} = num_text;
elseif i == 2 && j == 1
set(num_text, 'Parent', h2);
num_texts{2} = num_text;
elseif i == 3 && j == 1
set(num_text, 'Parent', h3);
num_texts{3} = num_text;
elseif i == 3 && j == 2
set(num_text, 'Parent', h6);
num_texts{6} = num_text;
elseif i == 2 && j == 2
set(num_text, 'Parent', h5);
num_texts{5} = num_text;
elseif i == 1 && j == 2
set(num_text, 'Parent', h4);
num_texts{4} = num_text;
end
end
end
```
这里使用了一个`cell`数组`num_texts`来存储每个箭头的数字文本对象。在添加数字时,根据箭头的位置将数字添加为相应箭头的子元素,并将数字文本对象存储到`num_texts`数组中的相应位置。这样,您就可以根据数字所在的箭头来旋转数字了。