Matlab changing word in character array

  • Thread starter Thread starter gfd43tg
  • Start date Start date
  • Tags Tags
    Array Matlab
AI Thread Summary
The discussion revolves around a MATLAB coding challenge to replace a substring within a character array using the `strfind` function and array concatenation. The initial code provided only works for specific cases and fails when different strings are used, highlighting the need for a more general solution. Participants suggest using the `numel` function and dynamically calculating indices for substring replacement instead of hardcoding values. A successful approach involves finding the starting index of `string1` and adjusting the concatenation to accommodate varying string lengths. The conversation emphasizes the importance of writing adaptable code and encourages exploring MATLAB documentation for built-in functions that could simplify the task.
gfd43tg
Gold Member
Messages
947
Reaction score
48

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
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
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.
 
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
:)
 
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
Back
Top