how to remove comma from certain items only from cell array with several items

hey all, i'am new to matlab and i have a query. i have a single cell array as following:
charstring={'"98,786","103,630","95,758","99,871","106,916",494,361,"92,793","81,741","79,919","79,919",999,989,999'}
this has some items in quotes and others without quotes. what i need to do is remove the ,(comma) character from only those numbers which are between quotes. so my final answer should look like:
charstring={'"98786","103630","95758","99871","106916",494,361,"92793","81741","79919","79919",999,989,999'}
i have tried all combination of strrep replaceBetween regexprep but could not get my head around this question. any help on this matter would be highly appreciated. thanks, azim

 採用された回答

Stephen23
Stephen23 2017 年 4 月 5 日
編集済み: Stephen23 2017 年 4 月 5 日
This is easy with regexprep:
>> str = {'"98,786","103,630","95,758","99,871","106,916",494,361,"92,793","81,741","79,919","79,919",999,989,999'};
>> out = regexprep(str,'("\d+),(\d+")','$1$2')
out =
"98786","103630","95758","99871","106916",494,361,"92793","81741","79919","79919",999,989,999

5 件のコメント

Andrei Bobrov
Andrei Bobrov 2017 年 4 月 5 日
+1
Jan
Jan 2017 年 4 月 5 日
編集済み: Jan 2017 年 4 月 5 日
+1, I like your REGEXP powers. The commands look cryptic, if one is not familiar with regular expressions. Therefore I tend to post a version with dull (so to say: Matlab 5) commands. The alternatives might be faster during the run-time, but more susceptible for typos due to the longer code (my first answer here contained 2 bugs...). In many cases saving a microsecond during the execution is less useful than spending minutes for debugging.
Stephen23
Stephen23 2017 年 4 月 5 日
編集済み: Stephen23 2017 年 4 月 5 日
@Jan Simon: thank you. I guess I practiced a lot, because I like how regexp (and friends) are quite fast, yet just requires some simple commands... or cryptic commands, if one is not used to them.
Here is a brief explanation of the above regular expression:
( start group 1
" match double quote
\d+ match one or more digits
) end group 1
, match comma
( start group 2
\d+ match one or more digits
" match double quote
) end group 2
So at this point we have matched this sequence: double-quote + digits + ',' + digits + double-quote, and have collected the quotes and digits into two groups. The output is thus as simple as '$1$2' which puts the contents of the two groups together. The comma is thus excluded as it was not in any of the groups, and parts of the string not in double quotes do not match the regular expression so are not affected.
Guillaume
Guillaume 2017 年 4 月 5 日
regular expression is like any other language (programming or otherwise). If you don't know it, it looks and sounds like gibberish. If you do know it, then it's easy to understand. Certainly, Stephen's expression here is very low on the complexity scale of regular expressions. It's only two capture groups each consisting of digits and " and separated by a comma. Yet, it does the job perfectly.
azim
azim 2017 年 4 月 5 日
beautiful answer...thanks for the help and the great explanation. i hope i become as good as you guys in coding.

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

その他の回答 (1 件)

Jan
Jan 2017 年 4 月 5 日
編集済み: Jan 2017 年 4 月 5 日
C = {['"98,786","103,630","95,758","99,871","106,916",494,361,"92,793",', ...
"81,741","79,919","79,919",999,989,999']}
D = strsplit(C{1}, ',');
quoted = strncmp(D, '"', 1);
D(quoted) = strrep(D(quoted), ',', ''); % [EDITED] Typos
Str = sprintf('%s,', D{:});
CleanC = {Str(1:length(Str)-1)}; % remove trailing comma

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

質問済み:

2017 年 4 月 5 日

コメント済み:

2017 年 4 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by