Matlab function that returns first and then last name

12 views (last 30 days)
Hello everyone; I need to create a function as follows:
function lcf = lastCommaFirst(name)
where name is a two part name consisting of a first name, a space, and a last name. You may assume that there is only one space between the two parts of the name and no other spaces in name. The value returned by lastCommaFirst is a string consisting of the last name, followed by a comma and a space, followed by the first name.
This is the code that I was able to create so far but I don’t seem to be able to make it work:
==============================
function lcf = lastCommaFirst2(name)
first = string1;
last = string2;
if name == string1 && name == string2
lcf = disp(last && ' ' && first);
end
==============================
I would really appreciate it if you can help me with this code using simple explanations as I am just a beginner at matlab programming; thanks.
  2 Comments
John
John on 2 Oct 2011
I was using them as values for (name) but that’s where matlab kept giving me errors, so I guess this is a mistake that I made in this code. I hope that this makes sense to you as I am just starting to learn about matlab programming.

Sign in to comment.

Accepted Answer

bym
bym on 2 Oct 2011
one way
s = 'joe smith'
lcf = [s(find(isspace(s))+1:end) ',' s(1:find(isspace(s))-1)]
lcf =
smith,joe
  1 Comment
John
John on 3 Oct 2011
Thanks proecsm your idea worked great; I really appreciate your help with this code.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 2 Oct 2011
Do not compare strings with == unless you are absolutely sure the both sides are exactly the same length. Instead, use strcmp() or isequal()

Community Treasure Hunt

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

Start Hunting!