How can I change contents of a string?

4 ビュー (過去 30 日間)
Mango
Mango 2012 年 7 月 31 日
I have a large cell array where the first column is a bunch of names like:
"Johnson and Johnson "
"Creative - Example "
"Problem/Cell"
They are all listed in data{1}
So, the main problem is that when I go to use the information in the "Problem/Cell" the "/" makes a lot of problems and is unusable. So, I would like to replace any "/" with a "-".
What I've done that hasn't worked is
newdata = data{1};
for i = 1:length(data{i})
position(i) = strfind(newdata(i),'/');
end
position = position';
if ~isempty(position)
newdata(position) = '-';
end
I get the error: Error using subsindex. Function 'subsindex' is not defined for values of class 'cell'. Error is in line newdata(position) = '-'
Thanks

回答 (3 件)

the cyclist
the cyclist 2012 年 7 月 31 日
If your array is A, then
A = regexprep(A,'/','-');
  1 件のコメント
Mango
Mango 2012 年 7 月 31 日
This was perfect, so much more simple than what I was doing. Thanks.

サインインしてコメントする。


Honglei Chen
Honglei Chen 2012 年 7 月 31 日
Is your newdata also a cell? If so, you need to access it's content via {}.
In your loop definition, you probably mean length(newdata) instead?
Finally, you may want to use strrep to replace the character.

Kevin Holst
Kevin Holst 2012 年 7 月 31 日
I'd say do something like this:
names = strrep({data{:,1}},'/','-');
That assumes that all of the names are only in the first column of your cell array as you said, and it makes a separate cell array with all of the names in it.

カテゴリ

Help Center および File ExchangeCustomize Object Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by