How to Check if a cell equals a word in a table

조회 수: 41 (최근 30일)
John Leben
John Leben 2021년 4월 15일
편집: Matt J 2021년 4월 15일
Does anyone know how to check a string in a cell in a table to detect if it is a specific word? For example, I have a table that has a bunch of rows. In each row is 1 word. I want to figure out how often a word appears in the table for a specific column.
My approach was to use a while loop with an if statement that would increase a variable every time the logic returned true. The logic is supposed to return true if the cell in the table is a specific string such as 'Aluminum' or 'plastic'. However, MATLAB does not allow me to use the == operator in a table. Does anyone know if there is a workaround?
My approach that did not work
x = readtable('litterati_processed_data.csv')
y = 1;
paper = 0;
otherstuff = 0;
while y <= 5936
if x(y, 11) == 'paper'
paper = paper + 1;
else
otherstuff = otherstuff + 1;
end
y + 1;
end

답변 (1개)

Matt J
Matt J 2021년 4월 15일
편집: Matt J 2021년 4월 15일
As an example,
x=cell2table({'dog','paper','cat','fish','paper'}.')
x = 5×1 table
Var1 _________ {'dog' } {'paper'} {'cat' } {'fish' } {'paper'}
paper=nnz( string(x{:,1})=="paper")
paper = 2
otherstuff=size(x,1)-paper,
otherstuff = 3

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by