Community Highlights

Community Pick

Adam Danz
448 views (last 30 days)

Happy St. Patrick's Day - simple emoticon & animation demo

Adam Danz 2021년 3월 17일 (2021년 3월 18일에 수정됨)
Latest activity Edit by Adam Danz 2021년 5월 24일

Did you know you can use most emoticons in text objects?

Most emoticons are just unicode characters. Using them as characters in Matlab is as simple as finding their numeric representation and then converting the numeric value back to character. Not all emoticons are convertible in Matlab.

Here's a secret message with emoticons. Use char(x) to decode it.

x = [79 77 71 33 32 55357 56878 32 104 97 118 101 32 121 111 ...
    117 32 117 112 100 97 116 101 100 32 116 111 32 77 97 116 ...
    108 97 98 32 55358 56595 32 114 50 48 50 49 97 32 121 101 116 32 8265];

Happy St. Patrick's Day!

fig = figure('MenuBar','none','Color', [0 .62 .376]);  % Shamrock green
ax = axes(fig,'Units','Normalized','Position',[0 0 1 1]);
axis(ax,'off')
axis(ax,'equal')
hold(ax,'on')
xlim(ax,[-1,1]); ylim(ax,[-1,1])
text(ax, 0, 0, char(9752), 'VerticalAlignment','middle','HorizontalAlignment','center','FontSize', 200)
str = num2cell('Happy St Patrick''s day!');
th = linspace(-pi/2,pi/2,numel(str)); 
txtHandle = text(ax,sin(th)*.8, cos(th)*.8, str, 'VerticalAlignment','middle','HorizontalAlignment','center','FontSize', 25);
set(txtHandle,{'rotation'}, num2cell(rad2deg(-th')))
thr = 0.017;
rotateCCW = @(xyz)([cos(thr) -sin(thr) 0; sin(thr), cos(thr), 0; 0 0 1]*xyz.').';
while all(isvalid(txtHandle))
    newposition = rotateCCW(vertcat(txtHandle.Position)); 
    set(txtHandle,{'position'}, mat2cell(newposition,ones(numel(txtHandle),1),3), ...
        {'rotation'}, num2cell([txtHandle.Rotation].'+thr*180/pi))
    drawnow()
end
 
Adam Danz
Adam Danz 2021년 5월 24일 (2021년 5월 24일에 수정됨)

Source: xkcd.com

 
Rik
Rik 2021년 3월 17일

I see nobody has posted the know-it-all correction yet, so I'll bite the bullet: emoticons are things like ¯\_(ツ)_/¯ and :), while emoji are the pictures in the Unicode standard (see the YouTube videos by Tom Scott for the interesting back story).

Also, this is one of the few areas where you can see Matlab uses UTF-16 to encode chars. Interestingly, the readlines function fails to read them correctly and replaces them by string(char(26)) (at least on R2020b, I haven't tested it on R2021a yet, this happens for all two-word characters as far as I can tell)

Can you expand on what you mean that not all emoji are convertible to Matlab? You mean the display is not correct?

 
1개의 답글
Adam Danz
Adam Danz 2021년 3월 17일 (2021년 3월 17일에 수정됨)

Interestingly, the current editor used to write this content uses UTF-8 which is why I had to use images of the emojis rather than the emojis themselves.

> Can you expand on what you mean that not all emoji are convertible to Matlab?

Example: Copy-paste the exploding head emoji

 
1개의 답글
Rik
Rik 2021년 3월 18일

I wonder why this is. Maybe the font used in the command window only has the glyphs for Unicode 6.0 (2010). At least Unicode 10.0 (2017) is apparently unimplemented. I checked for R2011a, and there the smiley face was missing as well. ML6.5 garbles a lot of special characters on display.

Unicode, encoding, and glyphs/fonts are quite a rabbit hole to fall into. I was 'forced' to do quite some digging and learning when writing my readfile and text2im FEX submissions.

Thank you for this nice write-up btw. I do enjoy these highlights blogs.

 
1개의 답글
Adam Danz
Adam Danz 2021년 3월 18일

@Rik, thanks for biting the bullet! Using the resource ImageAnalyst shared, I tested some emojis from Unicode Version 10.0 and Unicode Version 9.0 and it appears that the vs 9.0 is the cutoff in the Command window.

Here are two articles I found useful and entertaining.

 
Image Analyst
Image Analyst 2021년 3월 17일

If you want to pick your emoji from a visual list of all of them, you can see them all here:

https://emojipedia.org/people/

Click on the emoji you want, then click on the Copy button to copy it to the clipboard and then you can paste it wherever you want. Many/most places, like MATLAB Answers, will recognize them and display them properly.

 
1개의 답글
Adam Danz
Adam Danz 2021년 3월 17일

Also, for Windows 10 users, Win + period (.) or Win + semicolon (;) will bring up an emoticon menu.

 
David
David 2021년 3월 17일 (2021년 3월 17일에 수정됨)

Awesome post Adam :) And yes I have!

 

Community Treasure Hunt

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

Start Hunting!