Controlling the color of text in an array based on a predefined condition - matlab

I am trying to display a matrix M of size 50x8 with a plot using text([x,y],M). All the entries in matrix are at present of the same color. I would like more control on the display, and would like that all matrix entries satisfying a particular condition should be of different color.
One of the possible ways to do is to specify the position for each of the elements of the matrix M individually in text(x,y,M_ij). But I am only specifying the position for the first element, and other positions are being assigned automatically. How can I get those positions, or control them? This will allow me to control the colors as well.

The resulting text graphics object is only one object, so you can't adjust the color through handle graphics without affecting all of the rows. But if it's possible for you, you can specify the color directly in the strings. To do this, you'll probably need to represent your strings as a cell array instead, so they can have different colors.
M = {'\color{red}Line 1';'\color{blue}Line 2';'\color[rgb]{.6 .8 .2}Line 3'};
text(1, 1, M);
The reference for other inline string markup is found on this doc page, in the 'String' property: http://www.mathworks.com/help/matlab/ref/text_props.html

Related

Image segmentation algorithm in MATLAB

I need to implement an image segmentation function in MATLAB based on the principles of the connected components algorithm, but with a few modifications. This is intended for very simple, 2D images, with a background color and some objects in different colors.
The idea is that, taking the image as a matrix, I provide a tool to select the background color (it will vary for every image). Then, when the value of the color of the background of the image is selected, I have to segment all the objects in the image, and the result should be a labeled matrix, of the same size of the image, with 0's for the background, and a different number for each object.
This is a graphic example of what I mean:
I understand the idea of how to do it, but I do not know how to implement it on MATLAB. For each pixel (matrix position) I should mark it as visited and then if the value corresponds to the one of the background, assign 0, if not, assign another value. The objects can be formed by different colors, so in the end, I need to segment groups of adjacent pixels, whatever their color is. Also I have to use 8-connectivity, in order to count the green object of the example image as only one object and not 4 different ones. And also, the objects should be counted from top to bottom, and from left to right.
Is there a simple way of doing this in MATLAB? I know the bwlabel function, but it works for binary images only, so I'd like to adapt it to my case.
once you know the background color, you can easily convert your image into a binary mask of the same size:
bw=img!=bg_color;
Once you have a binary mask you can call bwlavel with 8-connectivity argument as you suggested yourself.
Note: you might want to convert your color image from RGB representation to an indexed image using rgb2ind before processing.

Matlab clustergram: add color markers by column label

I'm using clustergram in the Matlab bioinformatics toolbox. I want to add color markers to certain columns, similarly to this, but I want to mark specific ColumnLabels (IDs) rather than specific clusters.
Anyone know of a way to do that?
Found it. Based on Kevin's excellent suggestion, I passed a structure with colors for ColumnLabelsColor, then set LabelsWithMarkers as true:
clustergram(mat,'Colormap', redbluecmap,'ColumnLabelsColor',s,'LabelsWithMarkers',true)
mat is my DataMatrix. s is a structure of 2 cell arrays, each with length of the number of columns: first array is ColumnLabels, second array is my defined colors.
By default, setting ColumnLabelsColor changes the color of the text of the labels. Setting LabelsWithMarkers to true adds a color marker between the label and the clustergram:
Thank you for the suggestion. I have just figured out this problem.
First step: construct a structure with 'Labels' and 'Colors'. These two cell array should in the same length.
Second step: set the clustergram object, parameter 'ColumnLabelsColor' with this structure.
Also, set 'LabelsWithMarkers' as true.
[This is my original code for your reference ]
https://i.stack.imgur.com/ZTQ1h.png

matlab: variable horizontal alignment of text

Text objects in MATLAB contain a horizontal alignment property, which can be assigned a value of left, center, or right. Attempts to assign this property by a vector of alignments of equal length to the vectors of strings and coordinates fails to give the intended behavior.
For instance, a statement of the form :
text([1,1,1]/4,[1,2,3]/4,{'ABC';'BCD';'CDE'})
displays the contents of a length-3 cell array of char objects at the X- and Y-coordinates specified by length-3 double arrays. However, attempting to introduce a length-3 cell array of char objects for independent specification of the horizontal alignment of each text element is syntactically invalid;
e.g.,
text([1,1,1]/4,[1,2,3]/4,{'ABC';'BCD';'CDE'},'HorizontalAlignment',{'left';'center';'right'})
My question concerns whether it is possible to specify the HorizontalAlignment property of MATLAB text objects in a variable manner without resorting to constructs explicitly involving loops and conditionals.
You can't assign multiple property values upon creation, but once you have a vector of handles, you can use the many-to-many form of set() like so:
h = text([1,1,1]/4, [1,2,3]/4, {'ABC';'BCD';'CDE'});
set(h, {'HorizontalAlignment'}, {'left';'center';'right'});
The value array has one row per object, one column per property.

Change label of data tips on bode nichols diagram

When we plot a bode/nichols locus, the name of workspace variable is used
tmp=ss(1,1,1,0);
nichols(tmp);
will use 'tmp' as label.
When using more complex data, matlab is using 'untitled1','untitled2',...
tmp={ss(1,1,1,0) , ss(1.2,1,1,0)};
nichols(tmp{:});
How can I change this label programmatically?
Ideally, I'd like a solution working with Matlab 6.5.1, but I'm also interested in solutions restricted to newer versions.
You can modify the labels programmatically via their graphics handles. It looks like the values you want to change are the DisplayName property of some of the children of the current axis. So in your first example, I can change the display name like this:
ch = get(gca,'Children');
set(ch(1),'DisplayName','Fred');
In general, I'm not sure how to predict which children of the current axis are the ones you need to change. For the second example you give, the two curves appear to be the second and third children when I run your code.

Determine/Save the position of text in matlab

I plotted few points using scatter and then label them using text. The position of these labels are same as the position of the points + some offset. Some of these text label overlap with each other and hence I moved them interactively (using mouse). I can check the new position of each of these text individually using property editor. However this is very time-consuming. Is there a better way to get the coordinates of all these text-label?
You can use findobj to get handles to text objects that are children of the current axes (or another handle... your choice):
text_handles = findobj('parent',gca,'type','text');
Then you can get the positions of these text objects:
positions = get(text_handles,'position');
You may need to do a bit more work to associate each text object with its data point - I suggest taking advantage of the property system, perhaps via the UserData field, for this, though there are many options.
If you want to do it easily later do this in your plots, for example:
h=text(2.9,7.5,'MyText');
This will put "MyText" at position 2.9, and 7.5.
Then to change the position use:
set(h,'Position',[2.5 7]);
This will change the position to 2.5 and 7.
Later if you need to see tthe position of text again use:
get(h);
Hope this helps.