Matlab changing word in character array

  • Thread starter Thread starter gfd43tg
  • Start date Start date
  • Tags Tags
    Array Matlab
Click For Summary

Discussion Overview

The discussion revolves around a MATLAB programming problem where participants are tasked with writing code to replace the first occurrence of a substring within a character array. The focus is on using specific functions like strfind and array concatenation, and the conversation includes attempts at solutions, debugging, and improving code efficiency.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant presents an initial solution that works for a specific case but fails with different inputs, indicating a need for a more general approach.
  • Another participant critiques the initial solution for relying on hardcoded indices, suggesting that the program should dynamically determine these values using MATLAB functions.
  • A later reply introduces a modification to use the end keyword in the code, but expresses uncertainty about how to dynamically find the starting index for the substring.
  • Further contributions refine the solution by correctly calculating indices using strfind and numel, while also seeking ways to enhance the code's generality.
  • One participant raises a question about the behavior of the solution when the substring is not found, prompting a discussion about error handling and sensible outputs.
  • Another participant suggests exploring MATLAB's documentation for built-in functions that could simplify the task, encouraging self-directed learning.

Areas of Agreement / Disagreement

Participants generally agree on the need for a more robust solution that adapts to varying inputs, but there are differing opinions on the best approach to achieve this and the handling of edge cases.

Contextual Notes

Limitations include the initial reliance on hardcoded values, which restricts the solution's applicability to only certain cases. There is also uncertainty regarding the output when the substring is not present in the main string.

gfd43tg
Gold Member
Messages
949
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   Reactions: 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   Reactions: sysprog

Similar threads

Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
6K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K