Trying to create a table with a char array as variable names
古いコメントを表示
I'm trying to create a table that looks as follows:

with the whole alphabet and the number of occurences of each letter in the text.
As I have not yet understood how I can create a table with named variables, what I'm trying to do is as follows:
alphabet.letters = ('A':'Z')';
mytexttable = cell2table(cell(numel(texts(:,1)),28), 'VariableNames', {'Text', 'length', alphabet.letters'});
This only works if I change the row size of the table to 3, as this gives me the variables 'Text', 'length' and 'ABCDEF...'.
I tried a couple of other variations, including every combination of swirly, square and normal brackets and row/column arguments, 'A':'Z', char(cellstr(alphabet.letters)'), reshape(alphabet.letters, 1, 26) and a few more that I already forgot, but I only manage to get error messages or three rows with the third containing the whole alphabet.
I'm pretty sure I'm completely overlooking a rather easy solution, but I can't figure it out. Can you folks help me out, please?
採用された回答
その他の回答 (1 件)
This demo creates the table you described using random text. histcounts is used to count the number of each letter. The counts are case insensitive.
- texts is a cell array of random text.
- T is the output table.
% Generate fake text using natural frequencies of
% each letter in English
freq = [ 8.167 1.492 2.782 4.253 12.702 2.228 2.015 6.094 6.966 ...
0.153 0.772 4.025 2.406 6.749 7.507 1.929 0.095 5.987 6.327 ...
9.056 2.758 0.978 2.36 0.15 1.974 0.074]./100;
wordLen = randi(5,20,1);
nchar = randi(15,20,1)+6;
texts = arrayfun(@(n,w){char(randsample([32,'a':'z'],n,true,[.2,freq]))}, nchar, wordLen);
% Count number of characters in each text (spaces included)
length = cellfun(@numel, texts);
% Create preallocated table T
letterCounts = zeros(numel(texts),26);
bins = 97:123; % a - z (lower case) + 1 to cover last bin
letterNames = compose('%s',bins')';
tempTbl = array2table(letterCounts,'VariableNames', letterNames(1:end-1));
Text = string(texts);
T = [table(Text,length), tempTbl];
% Loop through each text, count each letter, populate table
for i = 1:numel(texts)
cnt = histcounts(double(lower(texts{i})),bins);
T{i,3:end} = cnt; % assumes 'a' starts in col 3
end
% Display results
T
カテゴリ
ヘルプ センター および File Exchange で Text Data Preparation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!