Replace char with other and count total numbers

Hello, I need some help.
1. I want to replace in string all the \n with whitespaces. I have already tried strrep(temp_str,'\n',' '); but it doesn't work!!
2. I want to count the number of zeros and ones in cells. For instance at the upper image i want as result number_of_zeros=3
number_of_ones=12
is there and way to do this?

回答 (4 件)

pfb
pfb 2015 年 4 月 13 日

1 投票

I do not see the relevance of strings or newline chars.
What is the object you're showing? What is the result of
whos x
where x is the object you represent graphically? If you could concatenate all of the vectors in "column" 2 into a single vector, say y, then the number of ones would be O=sum(y) and the number of zeros Z = length(y)-O.

3 件のコメント

pfb
pfb 2015 年 4 月 13 日
I'm not sure whether you have a cell with 2 columns... I do not use that graphical inspector.
Assuming it is a cell with 2 columns, and it is called x , try
O = sum(cell2mat(x(:,2)'));
Z = length(cell2mat(x(:,2)'))-O;
xaroula mav
xaroula mav 2015 年 4 月 14 日
Thanks a lot pfb it works!
Image Analyst
Image Analyst 2015 年 4 月 14 日
Go ahead and mark the answer as "Accepted" so he/she gets reputation points.

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

tdd4c1
tdd4c1 2015 年 4 月 13 日
編集済み: tdd4c1 2015 年 4 月 13 日

1 投票

1. this example might help you with your whitespace problem
str = 'hello\nworld';
formattedStr = sprintf(str)
newlineChar = sprintf('\n');
strrep(formattedStr,newlineChar,' ')
2. you can get number of zeros and ones by the following:
if your variable is x, then
number_of_ones = sum([x{:,2}]);
number_of_zeros = length([x{:,2}]) - number_of_ones;

3 件のコメント

xaroula mav
xaroula mav 2015 年 4 月 13 日
As about the number_of_ones and zeros i try what you suggested me but it doesn't work. It shows me the message "Undefined function 'sum' for input arguments of type 'cell' " Thanks a lot
tdd4c1
tdd4c1 2015 年 4 月 14 日
It appears that you are working with cell arrays. Make sure you use the curly braces and square brackets (not parens) when executing this code (e.g. [x{:,2}]). If this still doesn't work, post how you construct your data array.
xaroula mav
xaroula mav 2015 年 4 月 15 日
thanks a lot it works

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

xaroula mav
xaroula mav 2015 年 4 月 14 日

0 投票

As about the other question i have this variable which is depicted above. And i want to replace all the \n with whitespaces. Furthermore i want to convert it in cell array, is it possible? i looked everywhere but i found nothing. Thanks in advance
pfb
pfb 2015 年 4 月 14 日

0 投票

What do you exactly mean by "this variable which is depicted above" and where would the \n be?
I think tdd4c1 answered to your question about the newline above. Doesn't it work?
As to converting a string to char, I think it's enough to enclose it in braces
cell_a = {a}

2 件のコメント

xaroula mav
xaroula mav 2015 年 4 月 15 日
編集済み: xaroula mav 2015 年 4 月 15 日
sorry, I forgot to insert the image. No it didn't work what tdd4c1 suggested me.
pfb
pfb 2015 年 4 月 15 日
編集済み: pfb 2015 年 4 月 15 日
oh, ok... maybe attaching the mat file containing the char variable instead of its graphical representation would be more helpful in pinpointing the problem.
Even better would be a small portion of the variable that sufficient to exemplify the problem (a minimal example, that is).

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

カテゴリ

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

質問済み:

2015 年 4 月 13 日

編集済み:

pfb
2015 年 4 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by