1

I want to show direction for specific cells in a figure that I am reading from a matrix.

In the attached figure, I could create the left direction arrow, but I am not getting how to insert a diagonal arrow in the south west direction.

enter image description here

Apparently, latex arrow symbol works fine for left arrow, right arrow, up arrow and down arrow, but fails for diagonal arrows.

In the attached image I need to insert arrows in south west direction through cyan cells. How to do this?

Here is the script I am trying,

for row=1:size(data,1)
    for col=1:size(data,2);

        if data(row,col)==1   
            rectangle('Position',[col-0.5 row 1 1], 'FaceColor','y','EdgeColor','k', 'LineWidth', 0.1)
            text(col-0.95,row+0.6,'\leftarrow', 'fontsize', 6);

        elseif data(row,col)==2
            rectangle('Position',[col-0.5 row 1 1], 'FaceColor','c','EdgeColor','k', 'LineWidth', 0.1)
            text(col-0.95,row+0.6,'\swarrow', 'fontsize', 6);

        else
            rectangle('Position',[col-0.5 row 1 1], 'FaceColor','w','EdgeColor','k', 'LineWidth', 0.1)            
        end

    end

    set(gca,'Visible','off')  
end

Edit: Annotation option will increase the complexities and requires positioning of arrows for every another problem. If one could use latex arrow symbol the problem would become way more easier.

dsbisht
  • 1,025
  • 4
  • 13
  • 24
  • Did you consider to just draw the vectors, instead of using Latex text for it? – Bernhard May 08 '17 at 07:23
  • No, I have no idea. Could you elaborate a little bit more. – dsbisht May 08 '17 at 07:25
  • `annotation` may do the job – Ander Biguri May 08 '17 at 07:27
  • @AnderBiguri using `annotaion` would increase the complexities, as the arrow direction would change in other problems where I have noretheast, southeast, southwest, and northwest arrows. Moreover, positioning of arrows will further require lot of effort for every other problem. – dsbisht May 08 '17 at 07:36
  • 1
    Possible duplicate of [How to draw an arrow in Matlab?](http://stackoverflow.com/questions/25729784/how-to-draw-an-arrow-in-matlab) See especially [Marsei's answer](http://stackoverflow.com/a/25732096/5211833) – Adriaan May 08 '17 at 07:38
  • As a side-note: South-West is the lower left of an image. Are you sure you don't need it South-East (i.e. top left to lower right)? – Adriaan May 08 '17 at 07:43
  • @Adriaan I need diagonal arrows to southeast, southwest, northeast, and northwest direction, just like the horizontal arrows in west direction, shown in fig, for the ease of understanding I specifically asked for southwest arrows. Moreover I have 100s f arrows to show, hence, it would be difficult to use `annotation` option. – dsbisht May 08 '17 at 07:47
  • Hence, see the duplicate. Also, use your favourite search engine to search for `Stack overflow matlab arrow` and the first ten results are all good SO questions exactly like this problem. – Adriaan May 08 '17 at 07:49
  • 1
    Can you describe/show why does it fail with diagonals? – Ander Biguri May 08 '17 at 08:25
  • 3
    @Mario: I think you should consider looking at the `quiver` function. You won't need the ugly plot. Also, I think you should consider using `pcolor` as well. You can then get rid of your for loops and rectangles and annotations. – Bernhard May 08 '17 at 08:32
  • The `'\swarrow'` markup is not supported by MATLAB. The list of supprted characters can be found [here](https://www.mathworks.com/help/releases/R2017a/matlab/ref/text.html#inputarg_Interpreter). You will need a workaround like the ones already suggested. – souty May 08 '17 at 09:25
  • @Bernhard I need to show the flow direction using arrow. `pcolor` though efficient doesn't help for arrows, whereas `quiver` function gives different length for arrows based on their values, which should be uniform here. – dsbisht May 08 '17 at 10:44
  • @AnderBiguri `\swarrow` is not suppoerted by MATLAB unlike `\leftarrow`, though both are the latex arrow symbols. This is why diagonal arrow failed. – dsbisht May 08 '17 at 10:47
  • @Mario `pcolor` can easily replace the grid you are drawing, and in `quiver` you define the length yourself, so I don't see the problem. – Bernhard May 08 '17 at 10:51
  • @Bernhard thanks, I will try as you said. – dsbisht May 08 '17 at 11:01

1 Answers1

1

As suggested above, you can use quiver to draw the arrows. Also, pcolor is a much better way to plot all the rectangles and color them.

Here is an example for a code that use both of them to create something like what you want:

data = randi(3,10)-1; % some random data
% plot the rectangles:
pcolor([data data(:,end);
        data(end,:) 0])
% set the colors to w-y-c:
colormap([1 1 1; 
    1 1 0;
    0 1 1]);
[r, c] = ndgrid(1:size(data,1),1:size(data,2)); % a grid of all the cells
% logical indexing for the arrows:
leftarrow = (data==1);
swarrow = (data==2);
% plot all the arrows in black:
hold on
quiver([r r]+0.1,[c c]+0.5,[-leftarrow.' -swarrow.'],...
    [zeros(size(data)) -swarrow.'],'AutoScaleFactor',0.5,'Color','k')
hold off
set(gca,'Visible','off')

And a typical result: pcolor & quiver

EBH
  • 10,350
  • 3
  • 34
  • 59