Using fprintf to printing non-vowels in command window

Trying to adjust a previous program to print in the command window all the non-vowels from a inputted text file how do i fprint the code i have to show in the command window all the individual letters that arent vowles.
If text is "I love golf" command window should display like: lv glf
clear all
clc
inputfile = fileread('SampleText.txt');
Number_of_nonvowels = vowellesscounts(inputfile) % original
function w = vowellesscounts(s)
w=0;
l=length(s);
for i=1:l
if s(i)=='b' || s(i)=='c' || s(i)=='d' || s(i)=='f' || s(i)=='g' || s(i)=='h' || s(i)=='j' || s(i)=='k' || s(i)=='l' || s(i)=='m' || s(i)=='n' || s(i)=='p' || s(i)=='q' || s(i)=='r' || s(i)=='s'|| s(i)=='t' || s(i)=='v' || s(i)=='w' || s(i)=='x' || s(i)=='y'|| s(i)=='z'
w=w+1;
else
continue
end
end
end

 채택된 답변

Walter Roberson
Walter Roberson 2021년 9월 5일
function v = vowellesscounts(s)
v = '';
w=0;
l=length(s);
for i=1:l
if s(i)=='b' || s(i)=='c' || s(i)=='d' || s(i)=='f' || s(i)=='g' || s(i)=='h' || s(i)=='j' || s(i)=='k' || s(i)=='l' || s(i)=='m' || s(i)=='n' || s(i)=='p' || s(i)=='q' || s(i)=='r' || s(i)=='s'|| s(i)=='t' || s(i)=='v' || s(i)=='w' || s(i)=='x' || s(i)=='y'|| s(i)=='z'
w=w+1;
v(w) = s(i);
else
continue
end
end
end

댓글 수: 8

Note: your code does not achieve the documented purpose. Characters such as '$' are non-vowels, but they are not taken into account. Also, you are not taking into account upper-case characters.
Hint:
ismember('Now is the timing', 'Nni')
ans = 1×17 logical array
1 0 0 0 1 0 0 0 0 0 0 0 1 0 1 1 0
Hi @Walter Roberson, thanks for the resposne but i'm relatively new to matlab so not reallly sure what you are hinting at besides the upper case letters.
i still dont understand how to use the fprintf function to display each individual non vowel in the command window
Hint: displaying only the event numbers in a certain range
A = [2 7 3 8 7 6]
A = 1×6
2 7 3 8 7 6
B = 2:2:10
B = 1×5
2 4 6 8 10
mask = ismember(A,B)
mask = 1×6 logical array
1 0 0 1 0 1
A(mask)
ans = 1×3
2 8 6
thanks @Walter Roberson, sorry i get what you mean now, replace the matrix values with letters?
can you help me with fprintf at all?
result = A(mask);
fprintf('Yadda yadda, you wanted to know: %s\n', result)
@Walter Roberson i still cant get the fprintf working to display it how i explained in the question
inputfile = 'I love golf';
Number_of_nonvowels = vowellesscounts(inputfile); % original
fprintf('Edit this to say whatever you want: %s\n', Number_of_nonvowels);
Edit this to say whatever you want: lvglf
function v = vowellesscounts(s)
v = '';
w=0;
l=length(s);
for i=1:l
if s(i)=='b' || s(i)=='c' || s(i)=='d' || s(i)=='f' || s(i)=='g' || s(i)=='h' || s(i)=='j' || s(i)=='k' || s(i)=='l' || s(i)=='m' || s(i)=='n' || s(i)=='p' || s(i)=='q' || s(i)=='r' || s(i)=='s'|| s(i)=='t' || s(i)=='v' || s(i)=='w' || s(i)=='x' || s(i)=='y'|| s(i)=='z'
w=w+1;
v(w) = s(i);
else
continue
end
end
end
thanks alot, i was missing the 'number_of_nonvowel'
very much appreciated @Walter Roberson

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

릴리스

R2021a

태그

Community Treasure Hunt

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

Start Hunting!

Translated by