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!