Trying to create a table with a char array as variable names
52 ビュー (過去 30 日間)
古いコメントを表示
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?
0 件のコメント
採用された回答
Cris LaPierre
2021 年 11 月 15 日
Your letters need to be separated. One way to do this is put them in a colum vector, and turn that into a string. Note the other variable names will also have to be strings (double quotes) and you would use square brackets instead of curly braces.
letters=string(('A':'Z')')'
Actually building a table can be simple, but it would be helpful to know what data you already have. If you already have the data, and just want to turn it into a table, you could do this.
texts = ["any number of characters will do";"another text"];
Len = strlength(texts);
counts = randi(5,2,26);
mytexttable = table(texts,Len,counts);
mytexttable = splitvars(mytexttable,'counts','NewVariableNames',letters)
2 件のコメント
Cris LaPierre
2021 年 11 月 16 日
texts = ["any number of characters will do";"another text"];
len = strlength(texts)
num = count(texts,lettersPattern(1))
その他の回答 (1 件)
Adam Danz
2021 年 11 月 15 日
編集済み: Adam Danz
2021 年 11 月 15 日
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
参考
カテゴリ
Help Center および File Exchange で Tables についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!