where is the mistake?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
function oo(w)
if isstring(w)
count =0;
while count<15
if strcmp(w(count),'r')
disp('yes')
count=count+1;
end
end
end
0 comentarios
Respuestas (2)
David Hill
el 25 de En. de 2022
Why not just:
w='abcderfrgrhhr';
l=w=='r'
l =
1×13 logical array
0 0 0 0 0 1 0 1 0 1 0 0 1
0 comentarios
DGM
el 25 de En. de 2022
Editada: DGM
el 25 de En. de 2022
If you want it to do what your code implies:
w = 'a4jkltr6mramlkrffgajr';
oo(w)
function oo(w)
if isstring(w) % strings are not chars
w = char(w);
end
if ischar(w)
for c = 1:numel(w) % don't use a while loop, don't hard-code variable length
if w(c) == 'r' % don't need strcmp for single char tests
disp('yes') % if all you print is 'yes'
else
disp('no') % then there's no way to know where it occurred
end
end
end
end
If instead you want a function that's actually useful for something, @David Hill already posted a useful answer.
w = 'a4jkltr6mramlkrffgajr';
idx = letterisr(w)
function out = letterisr(w) % use a meaningful name and provide output
if isstring(w) % strings are not chars
w = char(w);
end
if ischar(w)
out = w == 'r';
end
end
0 comentarios
Ver también
Categorías
Más información sobre Line Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!