counting frequency of letters in text using matlab

suppose we have following text

s='i love georgia and its nature';

what i want is to count frequency of occurrence of each letter(space are not included of course) and sketch some chart(for example bar chart), first i have created code which counts letters using map container

 function character_count(s)
    % s is given string and given program will count  occurence of letters in
    % sentence
    MAP=containers.Map();% initialize   MAP for frequency counting
    n=length(s); % get length of given string
    letters=unique_without_space_sorting(s);
    for ii=1:n
        if ~isletter(s(ii))==1
            continue;
        elseif  isKey(MAP,s(ii) )
            MAP(s(ii)) = MAP(s(ii))  + 1;
        else
          MAP(s(ii)) = 1;

        end
    end
      y=values(MAP);
      y= cell2mat(y);
     bar(y);
    set(gca,'xticklabel',letters)

    end

here function

letters=unique_without_space_sorting(s);

returns cell array of letters of string s without sorting and space, here is its corresponding code

 function cell_stirng=unique_without_space_sorting(s)
    s=regexprep(s,'[^\w'']','');
     [~, idxs, ~] = unique(s, 'last');
     s= s(sort(idxs));
     n=length(s);
     cell_stirng=cell(n,1);
     for jj=1:n
         cell_string{jj}=s(jj);
     end
     end

when i run this code, i got following image counting frequency of letters in text using matlab

as you see there is no labels on x axis, how can i fix this issue? thanks in advance

You can use the first output of unique to give you the unique values and use those as your x labels

[values, idxs, ~] = unique(s, 'last');

% Make sure that they aren't sorted
[~, sortind] = sort(idxs);
values = num2cell(values(sortind));

% And later after creating your bar plot
set(gca, 'xtick', 1:numel(values), 'XTickLabels', values);

Or rather than doing that, you can just use the stable input to unique to make sure that they are in the order of appearance instead.

S = lower(strrep(s, ' ', ''));
[values, ~, b] = unique(S, 'stable');

hist(b, unique(b))
set(gca, 'xtick', 1:numel(values), 'xticklabels', num2cell(values))

Or if you want a histogram of all letters

S = lower(strrep(s, ' ', ''));
counts = histcounts(double(S), double('a':'z'));
bar(counts)
set(gca, 'xtick', 1:26, 'xticklabels', num2cell('a':'z'))

How about this as a simpler method?

str = 'i love georgia and its nature';

num_times = zeros(26,1);
letters = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', ...
           'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

for k = 1:length(str)

    % Convert letter to its lower case, get ASCII value, 
    % a = 97, b = 98, ..., z = 122
    n = uint16(lower(str(k)));

    % If character between 'a' and 'z'
    if n < 122 && n > 97

        % Convert to be between 1 and 26
        n = n - 96;

        % Index count array with n        
        num_times(n) = num_times(n) + 1;

    end

end

clf
stem(num_times);
set(gca, 'XTick', 1:26);
set(gca,'XTickLabel', letters)

Output:

counting frequency of letters in text using matlab

If you don't want to change your other code, see the last 2 lines of mine for labelling x-axis.

Edit:

You could use these lines for plotting instead of the above, so that only letters which have a non-zero frequency are plotted

clf 
stem(num_times(num_times ~= 0));    
set(gca, 'XTick', 1:sum(num_times ~= 0));
set(gca,'XTickLabel', letters(num_times ~= 0))

Output:

counting frequency of letters in text using matlab