Replace String Value in Cell with Numerical Value

3 ビュー (過去 30 日間)
chris1976
chris1976 2018 年 2 月 16 日
コメント済み: chris1976 2018 年 3 月 1 日
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 件のコメント
Guillaume
Guillaume 2018 年 2 月 16 日
Why does 'C.C4' get replaced by 2 and 'C.C5' by 3 (instead of 4 and 5 or something more logical)?

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

採用された回答

Stephen23
Stephen23 2018 年 2 月 19 日
編集済み: Stephen23 2018 年 2 月 19 日
A = {'Inf','C.C3','C.C2',1;'-Inf',1,'C.C5','Inf';'C.C4',0,0,'-Inf';0,0,1,'-Inf'};
idn = cellfun(@isnumeric,A); % identify numeric values.
out = nan(size(A)); % preallocate output matrix.
out(idn) = [A{idn}]; % allocate numeric values.
tmp = A(~idn); % subset with char vectors.
vec = str2double(tmp); % attempt to convert to numeric.
idx = isnan(vec); % identify char not converted.
C = {'C.C2','C.C3','C.C4','C.C5','C.C6'};
V = [ 10, 100, 34, 35, 100];
[idm,idc] = ismember(tmp(idx),C); % lookup table.
assert(all(idm),'Not in C:%s',sprintf(' %s,',C{~idm}))
vec(idx) = V(idc);
out(~idn) = vec
giving this numeric matrix:
out =
Inf 100 10 1
-Inf 1 35 Inf
34 0 0 -Inf
0 0 1 -Inf
An alternative to throwing an error for missing C.Cx strings would be to put a NaN in that position:
[idm,idc] = ismember(tmp(idx),C) % lookup table.
tmp = nan(size(idc));
tmp(idm) = V(idc(idm))
vec(idx) = tmp;
out(~idn) = vec
  1 件のコメント
chris1976
chris1976 2018 年 3 月 1 日
thx a lot stephen! your solution works (-:

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

その他の回答 (1 件)

Guillaume
Guillaume 2018 年 2 月 16 日
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 件のコメント
chris1976
chris1976 2018 年 2 月 19 日
Hi all! Thx for your answers. I couldn't solve my problem yet. I like to specify as follows:
My cell array looks for example like this (mixed string and numeric values out of xlsread):
A1 = {'Inf','C.C3','C.C2',1;'-Inf',1,'C.C5','Inf';'C.C4',0,0,'-Inf';0,0,1,'-Inf'}
I want to replace the following string values (variables) with numeric values for example:
C.C2 = 10 C.C4 = 34 C.C5 = 25 C.C6 = 100
And I like to change the string value "Inf" resp. "-Inf" to numeric Inf and -Inf (infinite and negativ infinite).
I tried with your solutions but couldn't do it - maybe because I specified my problem too little.
Hope you may help! Thx a lot in advance. Cheers, Chris

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

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by