Matlab changing word in character array

In summary, the goal is to write a code that uses the MATLAB function strfind and array concatenation to replace the first occurrence of string1 in sentence1 with string2, assigning the result to variable sentence2. The code should be able to work for different string and sentence lengths and positions. A possible solution is to use the strfind function to find the location of string1 in sentence1, and then use array indexing and the numel function to concatenate the correct parts of sentence1 with string2. It is important to consider how the code will handle cases where string1 is not found in sentence1. MATLAB also has a built-in function for string parsing that can be used for this problem.
  • #1
gfd43tg
Gold Member
950
50

Homework Statement


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'.

Homework Equations





The Attempt at a Solution



Here is my code.
Code:
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:
*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:
Physics news on Phys.org
  • #2
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:
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.
 
  • Like
Likes sysprog
  • #3
Hey AlephZero,

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

Code:
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:
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.
 
  • #4
I was able to get it with some help from a friend and came up with this

Code:
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
:)
 
  • #5
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
 
  • Like
Likes sysprog

1. How do I change a word in a character array in Matlab?

To change a word in a character array in Matlab, you can use the strrep function. This function takes in three inputs: the original character array, the word you want to replace, and the new word. Example: new_array = strrep(original_array, 'old_word', 'new_word').

2. Can I change multiple words in a character array at once in Matlab?

Yes, you can change multiple words in a character array at once in Matlab using the strrep function. Simply specify multiple words and their corresponding replacements as additional input arguments. Example: new_array = strrep(original_array, {'word1', 'word2'}, {'replacement1', 'replacement2'}).

3. How do I specify which instance of a word to replace in a character array in Matlab?

You can specify which instance of a word to replace in a character array in Matlab by using the strrep function with a fourth input argument. This argument specifies the number of times the replacement should occur. Example: new_array = strrep(original_array, 'word', 'replacement', 2) will only replace the first two instances of 'word' in the array.

4. Is there a way to change words in a character array without affecting the original array in Matlab?

Yes, you can use the strrep function with an additional output argument to store the modified array in a new variable without affecting the original array. Example: [new_array, modified] = strrep(original_array, 'old_word', 'new_word').

5. Can I change words in a character array using regular expressions in Matlab?

Yes, you can use regular expressions to change words in a character array in Matlab by using the regexprep function. This function takes in four inputs: the original array, the regular expression pattern to match, the replacement string, and optional flags. Example: new_array = regexprep(original_array, 'pattern', 'replacement').

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
6K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Back
Top