RTL msgbox in Matlab - matlab

I'm trying to present a msgbox in MATLAB which uses horizontal text alignment 'right'.
Problem is when I'm changing the text alignment, the message text appears out of the box.
h = msgbox('Sample Text');
th = findall(0, 'Tag','MessageBox' );
set(th, 'HorizontalAlignment', 'right');
Does anyone know to present the user with right-to-left message from Matlab?

The message string in the message box is actually created using the text function, i.e., it is positioned by two coordinates (neglecting the z-coordinate). To position it with the alignment right, you'll need to get the position of the message box and use its width argument to define the x position of the message text:
h = msgbox('Sample Text');
th = findall(0, 'Tag','MessageBox' );
boxPosition = get(h,'position');
textPosition = get(th, 'position');
set(th, 'position', [boxPosition(3) textPosition(2) textPosition(3)]);
set(th, 'HorizontalAlignment', 'right');
Since this puts your text exactly to the right border of the box, you'll need to adjust the text slightly to the left:
set(th, 'position', [boxPosition(3).*0.95 textPosition(2) textPosition(3)]);

Related

Matlab - Add a specific tick on a colorbar

I'm representing a surface using "surf" function, with a colorbar. I would like to keep the default ticks of the colorbar, but add a custom tick on this colorbar, at a specific value (that I could make red to distinguish it from other ticks for example). Any idea on how to add a custom tick like that with keeping existing ticks on the colorbar ?
Thanks
As Luis mentioned in the comments, you can add an additional tick mark like so
h = colorbar;
newTick = 0.75;
h.Ticks = sort([h.Ticks newTick]);
If you want to add a line to the bar, the easiest thing (I think) is to use an annotation which is positioned relative to the figure (the same as the colorbar), so we can overlay it
pos = h.Position;
r = (newTick - min(h.Ticks))/(max(h.Ticks)-min(h.Ticks));
annotation( 'line', pos(1)+[0, pos(3)], [1, 1]*(pos(2)+pos(4)*r), ...
'color', [1,0,0], 'linewidth', 2 );
I'm setting the x position of the annotation to match the left and right sides of the colorbar, and the y position to match the bottom plus the relative % of the height according to the tick value.
Result:
Similarly, you could use annotatation exclusively to just get a red label, it's a little more convoluted to get everything lined up correctly, you have to make sure the text box is wide enough to be on a single line and vertically aligned to the middle to get the position right:
h = colorbar;
newTick = 0.75;
pos = h.Position;
r = (newTick - min(h.Ticks))/(max(h.Ticks)-min(h.Ticks));
h = 0.2;
annotation( 'textbox', [pos(1)+pos(3)/2, (pos(2)+pos(4)*r)-(h/2), pos(3)*2, h], ...
'color', [1,0,0], 'string', ['- ' num2str(newTick)], 'linestyle', 'none', ...
'VerticalAlignment', 'middle' );

Text position based on another Text

I need to plot a vertical text (text 2) next to a horizontal text (text 1), if I use the the same position that I used to plot text1 they become superimposed.
I've tried to infer the second position based on the Extent property of text1, but I can't get the units right:
rec = txt.Extent;
pos_x = rec(1) + rec(3);
pos_y = rec(2);
text(pos_x,pos_y,txt2,'HorizontalAlignment','center','FontSize',sz,'Rotation',90,'Units','normalized');
I've tried also with units in pixels but that didn't worked either.
The figure is a time series, I couldn't find any convertion function.
From the documentation it seems to me that the Extent values are normalized but I dont know if anything else is needed to display the second position on those coordinates.
Does this look like what you want to do
plot with rotated text
Here is the code to make the above plot
plot(1:1.2)
h1 = text(1,1, 'Text String 1', 'verticalalignment', 'bottom');
rec = h1.Extent;
pos_x = rec(1) + rec(3);
pos_y = rec(2) + rec(4);
text(pos_x,pos_y,'Text String 2','HorizontalAlignment','left','FontSize',10,'Rotation',90,'verticalalignment', 'bottom');

Matlab plot marker label (NodeLabel) property

Is there any way to access and set plot marker property in Matlab?
In some case especially when user defined Marker is used (like the image below), it is necessary to set NodeLabel's position, font and color, to make it distinct in the figure.
g_obj = graph(sources, targets);
gp = plot(g_obj);
gp is a Matlab GraphPlot object and even though gp.NodeLabel is located in above layer, but has visual interference with user-defined markers' black lines and for example AL1, NAL1 and S6R2 are not readable.
Is there any way to set the Marker's font and position, using the gp itself?
I tried this solution which gives some flexibility, just copied the position and labels then used text instead of NodeLabel with more flexibility in color, font and etc.
%%---
gp = plot(graph_object,'Layout','layered');
labels = gp.NodeLabel;
gp.NodeLabel = [];
gp.LineStyle = 'none'; gp.Marker = 'none';
for i=1:length(labels)
text(gp.XData(i)+2, gp.YData(i)-5,labels(i),...
'fontsize', 8,'FontName', 'Arial', 'Color',[0 0.25 0],...
'FontWeight', 'bold');
end

How to wrap strings when plotting with `text`?

I have a long string that I would like to add to a subplot as descriptive text.
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.';
I have tried to add new line characters after every sentence to make it fit better.
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' ) ;
But it still does not fit properly within the axis.
Is there a way to wrap the string dynamically to fit the subplot?
How about using an annotate box, with the FitBoxToText property off?
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');
You can change the location by changing the 1st two elements of pos ,which (I think) describe the left-bottom corner, but forget.
You could use the textwrap function in one of two ways:
Wrap the text to fit within a text uicontrol:
hText = uicontrol('Style', 'Text', 'Position', ...(some starting position)... );
[wrappedText, newPosition] = textwrap(hText, {description});
set(hText, 'String', wrappedText, 'Position', newPosition);
Wrap the text at a fixed number of columns before plotting with text:
wrappedText = textwrap({description}, 20);
text(0.5, 0.5, wrappedText, 'FontSize', 14', 'FontWeight', 'Bold', ...
'HorizontalAlignment', 'center', 'VerticalAlignment', 'middle');

How to center text on a line

I would like to have a string of text that's centered on a line. I've tried this:
figure
axis([0,10,0,10])
d = 2.81;
center = 5;
line([center - d,center + d],[5,5])
th = text(center,4.9,'mmmmmmmmmmmmmmmmmmmmmm');
set(th,'HorizontalAlignment','center')
The text is aligned with the line on the right but not on the left. The above image is a screen shot. I did not consistently have this problem in saved versions of the figure.
Is there a way to center text on a line? I am not concerned about resizing the figure right now, but I would like to use the default font.
It seems that it's not possible to position text arbitrarily precise. I tried getting size of text and drawing line and re-positioning text accordingly. More about text properties here.
str1 = 'mmmmmmmmmmmmmmmmmmmmmm';
center = 5;
text_line_spacing = 0.2;
figure
axis([0,10,0,10])
% Set text initialy
th = text(0,0,str1);
% Get size of text
ext = get(th, 'Extent');
% text_width = ext(3);
% text_height = ext(4);
% Draw appropriate line
left = center - ext(3)/2;
right = center + ext(3)/2;
line([left right], [5 5])
% Reposition original text
set(th, 'Position', [left 5+text_line_spacing]);