indexing string in function

71 vues (au cours des 30 derniers jours)
Ian Tee
Ian Tee le 5 Mai 2016
Commenté : Ian Tee le 5 Mai 2016
I have a function that i want to return the first and last word from a string. so for example firstAndLast('matlab') should give me (m,b), however i am only able to get the first letter from this function?
function [first,last] = firstAndLast(instring)
first=instring(1);
last=instring(length(instring));
end
  6 commentaires
Stephen23
Stephen23 le 5 Mai 2016
Modifié(e) : Stephen23 le 5 Mai 2016
This is what your code does: I suspect that the problem is that were calling your function with only one output, instead of the two that it requires. In any case, in my answer I gave you a simpler way of doing this.
Ian Tee
Ian Tee le 5 Mai 2016
Thank you very much :) , turns out calling it was the problem.

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 5 Mai 2016
Modifié(e) : Stephen23 le 5 Mai 2016
Words
>> C = regexp('the quick brown fox','\S+','match');
>> C([1,end])
ans =
'the' 'fox'
Letters
>> fun = @(s)s([1,end]);
>> fun('matlab')
ans =
mb

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by