How do I find and replace all instances of a substring in a structure?

I have a structure that has a subject name all throughout it. I would like to replace every case of the subject name with another without going into each individual spot where it is. Is there an easy way to do this?

2 Comments

Please give us an example, which clears the term "a structure". A "structure which has a subject name" can be a variety of things.
Sorry, I'm extremely new to Matlab. In workspace, it says that it's a 1x6 cell, and each cell contains a struct, with each containing structs within it. The strings I want to replace are in cells or strings at different levels within the structs. I hope this clarifies my question.

Sign in to comment.

Answers (1)

If what you call a structure is the content of a char array (and not a struct object), you can use STRREP or REGEXPREP. Example:
>> str = 'Like other echinoderms, sea urchins are bilaterans. Their early larvae have bilateral symmetry,[3] but they develop fivefold symmetry as they mature. This is most apparent in the "regular" sea urchins, which have roughly spherical bodies, with five equally sized parts radiating out from their central axes. Several sea urchins, however, including the sand dollars, are oval in shape, with distinct front and rear ends, giving them a degree of bilateral symmetry. In these urchins, the upper surface of the body is slightly domed, but the underside is flat, while the sides are devoid of tube feet. This "irregular" body form has evolved to allow the animals to burrow through sand or other soft materials' ;
>> regexprep(str, 'symmetry', 'SYMMETRY')
ans =
Like other echinoderms, sea urchins are bilaterans. Their early larvae have bilateral SYMMETRY,[3] but they develop fivefold SYMMETRY as they mature. This is most apparent in the "regular" sea urchins, which have roughly spherical bodies, with five equally sized parts radiating out from their central axes. Several sea urchins, however, including the sand dollars, are oval in shape, with distinct front and rear ends, giving them a degree of bilateral SYMMETRY. In these urchins, the upper surface of the body is slightly domed, but the underside is flat, while the sides are devoid of tube feet. This "irregular" body form has evolved to allow the animals to burrow through sand or other soft materials
and STRREP leads to the exact same result. REGEXP(I/REP) is the most efficient/versatile tool available for pattern matching/replacement, but there is an overhead due to the fact that you build/call a regexp engine which can do much much more than just matching static strings (made of literals only). Therefore, if the only thing that you want to achieve is to replace a static string, use STRREP.

2 Comments

Based on you answer to Jan: are you talking about crawling through structs contained in a cell array? E.g. something like the following where you'd like to replace 'World' everywhere?
>> c = {struct('a', 'Hello World', 'b', 'Hello'), ...
struct('c', struct('e', 'Worldwide'), 'd', 'Hello')}
c =
[1x1 struct] [1x1 struct]
>> c{1}
ans =
a: 'Hello World'
b: 'Hello'
>> c{2}
ans =
c: [1x1 struct]
d: 'Hello'
>> c{2}.c
ans =
e: 'Worldwide'
If so, is the depth arbitrary?
Here is an example of such a crawler:
function obj = TaylorCrawler(obj, match, repl)
switch class(obj)
case 'cell'
obj = cellfun(@(c) TaylorCrawler(c, match, repl), obj, ...
'UniformOutput', false) ;
case 'struct'
fNames = fieldnames(obj) ;
for fId = 1 : length(fNames)
obj.(fNames{fId}) = ...
TaylorCrawler(obj.(fNames{fId}), match, repl) ;
end
case 'char'
obj = strrep(obj, match, repl) ;
end
end
Saving this function in TaylorCrawler.m, you get, when applied to the former example:
>> c2 = TaylorCrawler(c, 'World', 'WORLD')
c2 =
[1x1 struct] [1x1 struct]
>> c2{1}
ans =
a: 'Hello WORLD'
b: 'Hello'
>> c2{2}
ans =
c: [1x1 struct]
d: 'Hello'
>> c2{2}.c
ans =
e: 'WORLDwide'

Sign in to comment.

Categories

Tags

Asked:

on 16 Jul 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!