The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: What is the best way to replace certain characters in a string?
Replies: 4   Last Post: Sep 30, 2012 9:50 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]

Posts: 12
Registered: 9/29/12
What is the best way to replace certain characters in a string?
Posted: Sep 29, 2012 10:07 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Hello everyone,

I have a cell array of strings (each cell contains one string). What I need to do is to search for a certain key word in each cell and replace it with another.

For example,

a{1}='I like dogs.';
a{2}='I have three dogs.';
a{3}='One of the dogs is yellow.';

Now say I want to replace all the "dog" with "cat". In each string, the word "dog" occurs at most once. So I do the following

for m=1:length(a)
if ~isempty(k)
a{m}=[a{m}(1:k-1) 'cat' a{m}(k+1:end)];

It works but pretty slow for large cell array. Any suggestions on the implementation? I appreciate it.


Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.