Outputting cell vectors that contain specific information help?
Show older comments
I am trying to formulate a code that takes in input cell array of strings, evaluates the contents of each cell and outputs the cells that contain only uppercase letters
i.e.
cell =
3×2 cell array
{'ABC'} {'Abc'}
{'DEF'} {'dEf'}
{'GHI'} {'ghi'}
ans =
ABC
DEF
GHI
Here's my code so far:
function uppercase = all_caps(cell)
[rows, cols] = size(cell);
for ii = 1:rows
for jj = 1:cols
if contains(cell{ii, jj}, (['a', 'b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']) == 0
uppercase = [uppercase, cell{ii,jj}];
end
end
end
end
fprintf('%s\n', uppercase)
end
How can I use MATLABS assortment of built-in functions to prevent me from typing out each individual letter? Additionally, this method doesn't even work.
My professor showed us this solution:
function all_caps(c_in)
[rows, cols] = size(c_in);
for ii = 1:rows
for jj = 1:cols
str = c_in{ii, jj};
if sum(str >= 'A' & str <= 'Z') == length(str)
fprintf('%s\n', str);
end
end
end
end
I am confused about his if statement. I have no clue what the inside is going on inside the sum() function. Please help.
I know these are "two separate questions," but they are about the same function and regard the same topic.
Accepted Answer
More Answers (0)
Categories
Find more on Operators and Elementary Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!