I'm having some difficulties in finding specific codes for something I want in MATLAB, I have to do a job and in that work I load the bible file in .txt in MATLAB with this open file I can do some things with it:
TL = textread ('bible.txt', '% c') ---- With this command I can count the number of letters.
PT = texread ('bible.txt', '% s') ---- With this I count the number of words.
but my difficulty is as follows:
I want a command to count only specific letters in this great text .txt, for example a code that allows to count only the A or only the B, give thanks already to who has read so far. (Forgive my English the same is not the best.)

Antworten (2)

Image Analyst
Image Analyst am 31 Mär. 2019

0 Stimmen

Try this:
fileContents = fileread('bible.txt');
fileContents = fileContents - '!'; % Subtract ASCII ! to get numbers.
histogram(fileContents);
grid on;

4 Kommentare

Gabriel Cunha
Gabriel Cunha am 31 Mär. 2019
the graph was generated, however I wanted to get more or less result like this: A = number of letters A B = number of letters B
Walter Roberson
Walter Roberson am 31 Mär. 2019
Your original question asked to only count specific characters, not to produce a list of each unique contained character and a count of how many times it occurred.
Gabriel Cunha
Gabriel Cunha am 31 Mär. 2019
Forgiveness for the difficulty of expressing yourself
Image Analyst
Image Analyst am 31 Mär. 2019
Try this:
% fileContents = fileread('bible.txt'); % Read in file from disk
% Make known, sample array.
fileContents = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz B ZZZ'
fileContents = upper(fileContents) - 'A' + 1; % Subtract ASCII ! to get numbers.
edges = 1 : 27;
% Show how many of all letters are there via a bar chart.
histObject = histogram(fileContents, edges);
grid on;
% Find out the number of "b" or "B" in the file
% Pass the number of the letter in the alphabet as an index of the Values array of the histogram object.
numberOfB = histObject.Values(2)
% Find out the number of "z" in the file
numberOfZ = histObject.Values(26)

Melden Sie sich an, um zu kommentieren.

Walter Roberson
Walter Roberson am 31 Mär. 2019

0 Stimmen

[a, ~, aa] = find(accumarray(reshape(fileread('bible.txt'),[],1), 1));
fprintf('%c = %d\n', [a(:).'; aa(:).']);

Kategorien

Mehr zu App Building finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 31 Mär. 2019

Beantwortet:

am 31 Mär. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by