问题
I expect the text in the textbox to show up as:
Simulation parameters:
Number of loops = 500
Number of subcarriers = 12
Number of frames = 5,
MT = 2, MR = 2 % or MR x MT = 2 x 2
I have code written as:
txt = {'Simulation parameters: ','Number of loops = ', num2str(loops_num),'Number of subcarriers = ',num2str(Nfft),,'Number of frames = ',num2str(K),'MT = ',num2str(MT),'MR = ',num2str(MR)};
text(4,0.5,txt,'FontSize',12)
And I got:

1) How to fix it? (Number of subcarriers = 100
, etc need to be on one line)
2) How can I use annotation
in such a case?
plot(1:10)
str = {'Simulation parameters: ','Number of loops = ', num2str(loops_num),'Number of subcarriers = ',num2str(Nfft),'MT = ',num2str(MT),'MR = ',num2str(MR),'Number of frames = ',num2str(K)};
annotation( 'textbox', 'String',str, 'Color', 'black', ...
'FontSize', 14, 'Units', 'normalized', 'EdgeColor', 'none', ...
'Position', [0.8,0.5,0.2,0] )
set( gca, 'Position', [0.1, 0.1, 0.6, 0.8] )
回答1:
As is, you create a cell array of char arrays ("strings"), which is interpreted as separate lines when used in annotation
. So, you have to concatenate those values, which you want to have to be merged to one line by using [ ... ]
. See the following short example:
plot(1:10);
str = { ...
'Simulation parameters: ', ...
['Number of loops = ', num2str(500)], ...
['Number of subcarriers = ', num2str(12)], ...
['MT = ', num2str(2)], ...
['MR = ', num2str(2)], ...
['Number of frames = ', num2str(5)] ...
};
annotation('textbox', [0.3, 0.6, 0, 0], 'String', str, 'FitBoxToText', 'on');
The output (Octave 5.1.0; code also tested in MATLAB Online) looks like this:
Hope that helps!
EDIT: On the question, if the annotation could be moved outside the figure. It's possible, BUT incorporates manipulating the figure properties. That's the modified code:
plot(1:10);
set(gca, 'Position', [0.1 0.1 0.6 0.8]);
str = { ...
'Simulation parameters: ', ...
['Number of loops = ', num2str(500)], ...
['Number of subcarriers = ', num2str(12)], ...
['MT = ', num2str(2)], ...
['MR = ', num2str(2)], ...
['Number of frames = ', num2str(5)] ...
};
annotation('textbox', [0.75, 0.60, 0, 0], 'String', str, 'FitBoxToText', 'on');
That's the updated output:
回答2:
Matlab doesn't know you want to put the text and numbers together, since they are all individual strings, you need to combine the text and numbers together so Matlab knows to put them together, for example:
txt = {'Simulation parameters: ',['Number of loops = ', num2str(loops_num)],['Number of subcarriers = ',num2str(Nfft)],['Number of frames = ',num2str(K)],['MT = ',num2str(MT)],['MR = ',num2str(MR)]};
text(4,0.5,txt,'FontSize',12)
str = {'Simulation parameters: ',['Number of loops = ', num2str(loops_num)],...
['Number of subcarriers = ',num2str(Nfft)],...
['MT = ',num2str(MT)],...
['MR = ',num2str(MR)],...
['Number of frames = ',num2str(K)]};
annotation( 'textbox', 'String',str, 'Color', 'black', ...
'FontSize', 14, 'Units', 'normalized', 'EdgeColor', 'none', ...
'Position', [0.8,0.5,0.2,0] )
I combine the strings and variables using
[ 'text = ' num2str(value)];
You could also use
sprintf ( 'text = %f', value )
Its a bit more difficult to read, but its more powerful and a lot faster (for one off conversions this is irrelevant, but useful to know for future reference)
See sprintf documentation for more info
来源:https://stackoverflow.com/questions/59715170/multiline-annotation-problem-with-num2str