Replace String Value in Cell with Numerical Value
Show older comments
Hi all
I like to replace a string value in a cell with a numerical value.
The cell looks like:
A1 = {-1,0,0,0,0,0,0;1,-1,0,0,-1,0,0;0,1,1,-1,0,0,0;0,1,1,-1,0,0,0;0,0,1,0,0,0,0;0,0,0,'C.C4',0,0,0;0,0,0,0,1,1,-1;0,0,0,0,0,1,0;0,0,0,0,0,0,'C.C5'}
I like to replace the string C.C4 and C.C5 with numerical values like: C.C4 = 2; C.C5 = 3;
I already burnt hours on this problem! Thanks a lot for your help (-:
Cheers, Chris
1 Comment
Guillaume
on 16 Feb 2018
Why does 'C.C4' get replaced by 2 and 'C.C5' by 3 (instead of 4 and 5 or something more logical)?
Accepted Answer
More Answers (1)
Guillaume
on 16 Feb 2018
Identifying the char elements is easy. And once the rule for replacing them by numbers is known, replacing them is also easy:
toreplace = cellfun(@ischar, A1);
A1(toreplace) = num2cell((1:sum(toreplace(:))) + 1) %arbitrary rule that replace char arrays by numbers 2, 3, 4, ...
After that you probably want
A1 = cell2mat(A1)
1 Comment
chris1976
on 19 Feb 2018
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!