📄 txtblock.m
字号:
function h_text = txtblock(st_com_handle,p1,v1,p2,v2,p3,v3,p4,v4,p5,v5)
% The function TXTBLOCK allows for the graphical placement of a block of text.
%
% txtblock(string_block)
% txtblock(string_block,'linespacing',line_spacing)
% txtblock(string_block,'linespacing',line_spacing,'tag',tag)
% txtblock(string_block,'linespacing',line_spacing,'tag',tag,...)
%
% Make sure that the figure that you want the text to go into
% is the current figure. After the command has been executed
% you can move the mouse around in the figure window and a
% dashed box will indicate the extent of the text block.
%
% The string_block can be set up by passing either a matrix string
% or by separating lines in a string with the character "|".
%
% The line_spacing is defaulted to 1 (single space) but is at your
% disposal to change using (i.e., 1.5 = "one-and-a-half", 2 = "double",
% etc.)
%
% The text is placed in data units so that the text will stay in the
% axes relative to other graphics objects. Therefore it is recommended
% that to preserve the spacing, you first set the axes and then the
% text.
%
% The tag can be set (all text objects in the block will have the
% same tag so that if you want do find the handles of these text objects
% you can do a "handles = findobj('tag',tag)". The tag is a string of
% characters
%
% Examples:
%
% plot(-20:10);
% txtblock('Line 1|This will be line 2')
%
% h = txtblock('First line|Second Line|Third Line',...
% 'linespacing',1.5);
% SEE ALSO STR2MAT, GTEXT, and TEXT
% Patrick Marchand (pmarchan@motown.ge.com) 10 Jan. 1994 Initial
if rem(nargin,2) ~= 1
error('Error: Improper usage of put tblk3.m');
end
string_matrix = [];
if isstr(st_com_handle)
string_matrix = st_com_handle;
command = 1;
else
handle = eval('findobj(st_com_handle,''flat'',''type'',''text'')','[]');
if length(handle) == 0
command = st_com_handle;
else
command = 1;
end
end
% DETERMINE LINE SPACING
line_spacing = [];
block_position = [];
tag_value = [];
arg_str = [];
tmp_com_str = [''];
for loop = 2:2:(nargin-1)
p = eval([sprintf('p%d',loop/2)]);
v = eval([sprintf('v%d',loop/2)]);
if length(p) < 3
error('Unrecognized property in txtblock.m')
end
if strcmp(lower(p(1:3)),'lin')
line_spacing = v;
elseif strcmp(lower(p(1:3)),'pos')
block_position = v;
elseif strcmp(lower(p(1:3)),'tag')
tag_value = v;
else
tmp_com_str = [tmp_com_str ',' [sprintf('p%d',loop/2)] ',' ...
[sprintf('v%d',loop/2)]];
end
end
if length(line_spacing) == 0
line_spacing = 1;
end
if length(tag_value) == 0
tag_value = 'block_text';
end
if command == 1
if length(string_matrix) == 0
%keyboard
for loop = handle'
if loop == handle(1)
string_matrix = [get(loop,'string')];
else
string_matrix = [string_matrix '|' get(loop,'string')];
end
end
delete(handle);
end
h = uicontrol('style','text','string',string_matrix,...
'visible','off','tag','put_tblk_tmpui');
string_matrix = get(h,'string');
set(h,'string',tag_value);
old_0_units = get(0,'units');
old_f_units = get(gcf,'units');
old_a_units = get(gca,'units');
set(0,'units','points');
pt_m = get(0,'PointerLocation');
set(gcf,'units','points');
figpos = get(gcf,'pos');
set(gca,'units','points');
axpos = get(gca,'pos');
pt_m = pt_m - figpos(1:2);
pt = pt_m;
% DETERMINE EXTENT OF BOX
t_test = text(pt(1),pt(2),string_matrix(1,:),...
'units','points','visible','off');
fts_test = get(t_test,'fontsize');
ext_test3 = 0;
for loop = 1:size(string_matrix,1)
set(t_test,'string',string_matrix(loop,:));
ext_test = get(t_test,'extent');
if ext_test(3) > ext_test3
ext_test3 = ext_test(3);
end
end
pt(2) = pt(2)-0*(ext_test(4)-fts_test)/2;
ext_test(3) = ext_test3-(ext_test(4)-fts_test)/2;
ext_test(4) = size(string_matrix,1)*fts_test*line_spacing;
delete(t_test);
set(h,'userdata',[fts_test line_spacing]);
% CREATE BOX IN DATA UNITS
box_x = [0 0 ext_test(3) ext_test(3) 0]+pt(1);
box_y = [0 -ext_test(4) -ext_test(4) 0 0]+pt(2);
sc_p2dx = (diff(get(gca,'xlim')))/axpos(3);
sc_p2dy = (diff(get(gca,'ylim')))/axpos(4);
box_xd = (box_x-axpos(1))*sc_p2dx+min(get(gca,'xlim'));
box_yd = (box_y-axpos(2))*sc_p2dy+min(get(gca,'ylim'));
bx = line(box_xd,box_yd,'linestyle','--','clipping','off',...
'color',[.7 .7 .7],'eras','xor','tag','put_tblk_outline',...
'userdata',string_matrix);
% DEFINE MOUSE MOVEMENT CALLBACK
if length(block_position) == 0
str = [
'pt_m = get(0,''PointerLocation'');' ...
'figpos = get(gcf,''pos'');' ...
'axpos = get(gca,''pos'');' ...
'pt_m = pt_m - figpos(1:2);' ...
'pt = pt_m;' ...
'sc_p2dx = (diff(get(gca,''xlim'')))/axpos(3);' ...
'sc_p2dy = (diff(get(gca,''ylim'')))/axpos(4);' ...
'bx = findobj(gcf,''tag'',''put_tblk_outline'');' ...
'bx_xd = get(bx,''xdata'');' ...
'bx_xd = bx_xd - min(bx_xd);' ...
'bx_xd = bx_xd + (pt(1)-axpos(1))*sc_p2dx+min(get(gca,''xlim''));' ...
'bx_yd = get(bx,''ydata'');' ...
'bx_yd = bx_yd - max(bx_yd);' ...
'bx_yd = bx_yd + (pt(2)-axpos(2))*sc_p2dy+min(get(gca,''ylim''));' ...
'set(bx,''xdata'',bx_xd,''ydata'',bx_yd);'...
];
set(gcf,'WindowButtonMotionFcn',str);
waitforbuttonpress;
else
old_pt_pos = get(0,'pointerlocation');
block_position(1) = (block_position(1)-min(get(gca,'xlim')))/sc_p2dx + axpos(1) +figpos(1);
block_position(2) = (block_position(2)-min(get(gca,'ylim')))/sc_p2dy + axpos(2)+figpos(2);
set(0,'pointerlocation',block_position);
end
%elseif command == 2
bx = findobj(gcf,'tag','put_tblk_outline');
h = findobj(gcf,'tag','put_tblk_tmpui');
tag = get(h,'string');
string_matrix = get(bx,'userdata');
ax = get(bx,'parent');
pt_m = get(0,'PointerLocation');
figpos = get(gcf,'pos');
axpos = get(ax,'position');
pt_m = pt_m - figpos(1:2);
pt = pt_m;
sc_p2dx = (diff(get(ax,'xlim')))/axpos(3);
sc_p2dy = (diff(get(ax,'ylim')))/axpos(4);
h_ud = get(h,'userdata');
fts_test = h_ud(1);
line_spacing = h_ud(2);
HoldStatus = ishold;
hold on;
ptt = pt;
xlim = get(ax,'xlim');
ylim = get(ax,'ylim');
for loop = 1:size(string_matrix,1)
t_xpos = (ptt(1)-axpos(1))*sc_p2dx+min(xlim);
t_ypos = (ptt(2)-axpos(2))*sc_p2dy+min(ylim);
t(loop)=text(t_xpos,t_ypos,string_matrix(loop,:),...
'vert','top','tag',tag);
ptt(2) = ptt(2) - fts_test*line_spacing;
end
if length(tmp_com_str) > 0
eval(['set(t' tmp_com_str ');'],'error(''Unrecognized Property'')')
end
if HoldStatus == 0
hold off;
end
if length(block_position) ~= 0
set(0,'pointerlocation',old_pt_pos);
end
set(0,'units',old_0_units);
set(gcf,'WindowButtonUpFcn','',...
'WindowButtonMotionFcn','',...
'units',old_f_units);
set(gca,'units',old_a_units);
if nargout > 0
h_text = t;
end
delete(bx);
delete(h);
refresh
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -