Help displaying text to a figure.

27 ビュー (過去 30 日間)
Peter
Peter 2014 年 3 月 11 日
コメント済み: Peter 2014 年 3 月 12 日
If I do not want to plot an actual graph, and just display text of different colors as if it was written in a text editor or something similar how would I do that? So I wrote this program that deals random cards out. The program works great, but I don't know how to do the display. Here is the program though really all I am asking is for help with the figure.
function []=myShowHand(cards,PlayerName)
for k = 1:length(cards)
if cards(k) == 1
cards(k) = 13;
elseif cards(k) == 14
cards(k) = 26;
elseif cards(k) == 27
cards(k) = 39;
elseif cards(k) == 40
cards(k) = 52;
else
cards(k) = cards(k) - 1;
end % nested if
end % for loop
cards = sort(cards,'descend');
A = find(cards <= 13);
AA = cards(A); %clubs
B = find(cards >= 14);
BB = cards(B);
C = find(BB <=26);
CC = cards(C); %diamonds
D = find(cards >= 27);
DD = cards(D);
E = find(DD <=39);
EE = cards(E); %hearts
F = find(cards >= 40);
FF = cards(F); %spades
clubs = AA;
diamonds = CC;
hearts = EE;
spades = FF;
deck = '23456789TJQKA23456789TJQKA23456789TJQKA23456789TJQKA';
clubs = deck(clubs);
diamonds = deck(diamonds);
hearts = deck(hearts);
spades = deck(spades);
figure
axis off
text(1,1,'\spadesuit')
text(1,2,'\heartsuit')
text(1,3,'\diamondsuit')
text(1,4,'\clubsuit')
end % function
  1 件のコメント
Peter
Peter 2014 年 3 月 11 日
編集済み: Peter 2014 年 3 月 11 日
So I want it to display this basically
*Player Name*
(Spade) J875
(Heart) KT64
(Diamond) QJ842
(Club) AKQJ952

サインインしてコメントする。

採用された回答

dpb
dpb 2014 年 3 月 11 日
text will work just fine, it works in a default axes object. Start with sotoo...
s={'(Spade) J875';'(Heart) KT64';'(Diamond) QJ842';'(Club) AKQJ952'}
hF=figure; set(hF,'color',[1 1 1])
hA=axes; set(hA,'color',[1 1 1],'visible','off')
text(0.5,0.5,s)
where you dynamically build the s array to write by combining the suit names and card holding in cell array. I just copied your example for playing with. cellstr can be your friend here.
You can then play with the figure size, location, as well as the scale for the axes (default 0-1 above) and text has all the properties you need for modifying at will. See the doc for it for that...
  3 件のコメント
Peter
Peter 2014 年 3 月 11 日
The program for the assignment has certain things that need to work in certain ways. I had to make everything general enough to work outside of the most simplest thing. For instance a 1 has to be an Ace but an Ace has to be higher than a King. Needs to be descending. I'm not awesome at coding but this seems to work for it. Also, I got it all to work but how can I change the color of the text for diamonds and hearts to red? I know how to change the color of all of 's' but I want clubs and spades text to remain black.
Peter
Peter 2014 年 3 月 11 日
function []=myShowHand(cards,PlayerName)
for k = 1:length(cards)
if cards(k) == 1
cards(k) = 13;
elseif cards(k) == 14
cards(k) = 26;
elseif cards(k) == 27
cards(k) = 39;
elseif cards(k) == 40
cards(k) = 52;
else
cards(k) = cards(k) - 1;
end % nested if
end % for loop
cards = sort(cards,'descend');
A = find(cards <= 13);
AA = cards(A); %clubs
B = find(cards >= 14);
BB = cards(B);
C = find(BB <=26);
CC = cards(C); %diamonds
D = find(cards >= 27);
DD = cards(D);
E = find(DD <=39);
EE = cards(E); %hearts
F = find(cards >= 40);
FF = cards(F); %spades
clubs = AA;
diamonds = CC;
hearts = EE;
spades = FF;
deck = '23456789TJQKA23456789TJQKA23456789TJQKA23456789TJQKA';
clubs = ['\clubsuit', deck(clubs)];
diamonds = ['\diamondsuit', deck(diamonds)];
hearts = ['\heartsuit' ,deck(hearts)];
spades = ['\spadesuit', deck(spades)];
s = {PlayerName; spades ;hearts ;diamonds ;clubs};
myFigure = figure;
set(myFigure,'color',[1 1 1])
axis off
text(0,.5,s,'FontSize',14)
end % function
This works to give me everything listed the way I want. Two problems. I want it centered up top of the figure, and diamonds and hearts to be red text. I keep playing with it but I just can't get it to do this.

サインインしてコメントする。

その他の回答 (1 件)

dpb
dpb 2014 年 3 月 12 日
編集済み: dpb 2014 年 3 月 12 日
See links from
doc text
for all the properties of the text object and examples of using them.
...want it centered...
'horizontalAlignment','center
...and diamonds and hearts to be red...
'color', 'r'
For the latter you'll have to split the text strings to write the black suits and red suits individually.
Or, you can get more fancier and use TeX and the \color and other directives.
See the section in documentation on Adding Text Annotations to Graphs in the Graphics chapter for lots of examples and the full documentation story.
You can just try the following for starters
text(0.5,0.5,'Clubs','horizontal','center','color','k')
text(0.5,0.45,'Hearts','horizontal','center','color','r')
or
txtstr(1)={'\color{black} Clubs '};
txtstr(2)={'\color{red} Hearts '}
text(0.5,0.5,txtstr,'interpreter','tex','horizonatalalign','center')
Obviously you'll want to build the strings for the hands dynamically.
  1 件のコメント
Peter
Peter 2014 年 3 月 12 日
Thanks for all for all of your help. I will give it a try.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by