1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab changing word in character array

  1. Jun 14, 2014 #1

    Maylis

    User Avatar
    Gold Member

    1. The problem statement, all variables and given/known data
    Let sentence1, string1, and string2 be three variables whose values are character strings.
    Using only the MATLAB function strfind and array concatenation, write code that will replace
    the first occurrence of string1 in sentence1 with string2, assigning the result to variable
    sentence2.

    sentence1 = 'Prof. Papadopoulos is an E7 instructor';
    string1 = 'Papadopoulos';
    string2 = 'Packard';

    then after executing the correct code, the variable sentence2 will be 'Prof. Packard is an
    E7 instructor'.
    2. Relevant equations



    3. The attempt at a solution

    Here is my code.
    Code (Text):
    sentence1 = 'Prof. Papadopoulos is an E7 instructor';
    string1 = 'Papadopoulos';
    string2 = 'Packard';

    strfind(sentence1, 'Papadopoulos');
    strfind(sentence1, 'is');
    size(sentence1);
    sentence2 = [sentence1(1:6), string2, sentence1(19:38)];
    sentence2

    sentence2 =

    Prof. Packard is an E7 instructor
    Here is the error message I receive when I run the autograder.

    Code (Text):
    *your code did not execute successfully for the variables: sentence1 = 'Prof. Packard is an E7 instructor'; string1 = 'an E7'; string2 = 'a ME231';
    *your code did not execute successfully for the variables: sentence1 = 'Prof. Packard is an E7 instructor'; string1 = 'Prof.'; string2 = 'Dr.';
    It appears that the autograder will check the string by replacing with different words, and my code is supposed to work even with word switches. So something about my code only works in this particular case, but if the sentence is changed it is not right in general.
    It does give me the sentence, but when I run the autograder, this is the error message.

    A hint I got was somehow to incorporate the numel function into my code. That just tells me the number of elements, so I don't see how that will aid me as of yet.
     
    Last edited: Jun 14, 2014
  2. jcsd
  3. Jun 14, 2014 #2

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    Your code has missed the point of writing a computer program, which is to get the computer to do the grunt work, not do it yourself!

    Apparently, you counted the characters by hand, and in the statement
    Code (Text):

    sentence2 = [sentence1(1:6), string2, sentence1(19:38)];
     
    the 6, 19, and 38 only work for a string of exactly that length at exactly that place in the sentence.

    What you were supposed to do was get the computer to find out the length sentence1 and string1, and where string1 starts in sentence1. Then get the computer to do the math to find the correct values corresponding to your 6, 19, and 38, and paste the correct pieces of text together. You need to use three variables instead of your constants 6, 19, and 38.
     
  4. Jun 15, 2014 #3

    Maylis

    User Avatar
    Gold Member

    Hey AlephZero,

    I only changed one small part of the code (replaced 38 to end to be more general)

    Code (Text):
    sentence2 = [sentence1(1:6), string2, sentence1(19:end)]
    But I am stuck on how I will be able to find the variable for 6 and 19 to put into the command.

    This is what I thought up of right now
    Code (Text):
    A = strfind(sentence1, string1)

    A =

         7

    EDU>> sentence2 = [sentence1(1:A),string2,sentence1(string2:end)]
    For colon operator with char operands, first and last operands must be char.
     
  5. Jun 15, 2014 #4

    Maylis

    User Avatar
    Gold Member

    I was able to get it with some help from a friend and came up with this

    Code (Text):
    A = strfind(sentence1, string1);
    sentence2 = [sentence1(1:A-1),string2,sentence1(A+numel(string1):end)];
     
    However, I want to make my code even better. I had to think for myself that I have to subtract 1 from A to know the location to input string2, as well as A+numel(string1) to end string2. How can I get the computer to do this for me?

    Can you think of an even better way to do this? I want to one up my friend with an even more general code
    :)
     
  6. Jun 15, 2014 #5

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    That looks perfectly OK to me. It works, and it's easy to see what it does by reading the code.

    The question doesn't say what is supposed to happen if "string1" is NOT found in "sentence". Find out what your solution does in that case. If it doesn't give an error message, is the result somethng "sensible"?

    There is always a tradeoff in computer programming between having built-in functions for everything you might possibly want to do (but you can never remember exactly which function you need), and having one function that gives you the essential information, but you might have to add a few details (like the "-1") yourself.

    Actually, matlab has a function that does everything you need for this question. Since "learning to program" also involves "learning how to find stuff in the documentation", try to find it yourself and figure out how to use it. See http://www.mathworks.co.uk/help/matlab/string-parsing.html
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Matlab changing word in character array
Loading...