Matlab - Dealing With Strings

Discussion in 'Programmer's Corner' started by Judas543, Mar 27, 2010.

  1. Judas543

    Thread Starter Member

    Jan 26, 2010
    60
    0
    [​IMG]


    clc %clear command
    clear %clear workspace

    Roster = []


    %Infinite while-end loop (1 == True)
    while (1)
    %Create a menu of options. (Steps 1-2) (Given in the homework)
    fprintf('\n1 - Enter Names In Roster \n');
    fprintf('2 - Quit \n');



    %Input a choice number (1-4 for menu)
    choice = input('\n Please enter your choice:');

    switch choice%Switches to choice case
    %The user selects an option and the system displays the corresponding result/function (Given to us in the lab)
    case 1
    Roster = input('Please enter a [first_name, blank, last_name] using the char function:');
    Roster

    %For case 2, it breaks and displays "Quitting"
    case 2
    fprintf('Quitting! \n'); %Print message "Quitting!"
    break

    %If the user enters a value outside the [1 – 2] range, the system displays the error message "Invalid Entry"
    %Could also use disp('Invalid Entry')
    otherwise
    %If input any number that isn't (1 to 2) Then display (Invalid
    %Option) and go back to the main menu
    disp('invalid option')
    end
    end


    + I need help if I am doing this right, I think I have the menu down, and you have to enter the names as a string. e.g: char('bob dylan',walt disney')
    But now i have a problem, how do I re arrange the names that so it will display lastname firstname. Originally the user types in firstname lastname. (use slicing and concatenation is a hint)

    next I need to know how to make the uppercase of the initials....

    After that I should be set in trying to do the homework. It's due on March 29, but I need to understand it since i have an exam on thursday April 1
     
  2. johndoe45

    Member

    Jan 30, 2010
    364
    1
    can't help you there. but will look into it. always look forward to learning more.

    we just mess with numbers. haven't done text.

    that's cool. now i know how to do menu as well. sort of

    SO WHAT VARIABLES AM I SUPPOSED TO BE USING SO I CAN RESEARCH THEM??????? I KNOW CHAR IS ONE.

    only thing i don't understand with your menu is how to display the roster backwards and with upper case if after you type in names on roster it just keeps asking you for names or to quit again??????

    are you just supposed to type in the names and then it displays it right there and then upper case and backwards?????
     
    Last edited: Mar 28, 2010
  3. Ghar

    Active Member

    Mar 8, 2010
    655
    72
    Matlab has a lot of good string functions, pretty much everything you get with C but even more flexible usually.

    Look into strtok which separates a string into many based on a delimiter character you pick.
    Here's the Matlab reference for String functions:
    http://www.mathworks.com/access/helpdesk/help/techdoc/ref/f16-42340.html#f16-51157

    While you can use array notation to combine strings, strcat is useful because it strips whitespace.

    There's also strvcat, which lets you put different length strings into rows of an array.
     
  4. Judas543

    Thread Starter Member

    Jan 26, 2010
    60
    0
    yea thats the idea
     
Loading...