How can I strip single quotes from a cell array?

조회 수: 15 (최근 30일)
gsourop
gsourop 2018년 10월 26일
편집: Stephen23 2018년 10월 26일
Hi everyone,
I am trying to get rid of the single quotes from a cell array elements in order paste results on TEX. In addition, the problem is that the characters inside a cell array are not the same. For example, as you can see, in the example below, a{1} has 7 characters and a{2} has 6 (including quotes).
a{1} = '0.05*';
a{2} = '0.10';
a{3} = sprintf('%s%s%s','\textbf{',0.33,'}');
I have tried
for i = 1:2
D{i} = strrep(a{i}, '''', '')
end
But doesn't seem to be the solution. The expected outcome would be 0.05*, 0.10 and \textbf{0.33}.
  댓글 수: 1
Stephen23
Stephen23 2018년 10월 26일
편집: Stephen23 2018년 10월 26일
"For example, as you can see, in the example below, a{1} has 7 characters and a{2} has 6 (including quotes)."
Lets check them using MATLAB:
>> numel(a{1})
ans = 5
>> numel(a{2})
ans = 4
I can't find any quotation characters (character code 39) anywhere in those character vectors:
>> any(a{1}==39)
ans = 0
>> +a{1}
ans =
48 46 48 53 42
"But doesn't seem to be the solution"
Because you are trying to remove something that is not there.
"The expected outcome would be 0.05*, 0.10 and \textbf{0.33}."
That is exactly what I get (once I fixed your sprintf format string):
>> a{:}
ans = 0.05*
ans = 0.10
ans = \textbf{0.33}

댓글을 달려면 로그인하십시오.

답변 (1개)

Jan
Jan 2018년 10월 26일
편집: Jan 2018년 10월 26일
Your cell array does not contain any quotes, so removing them has no effect. The quotes appear only in the command window, when the cell array is displayed. This is a smart idea to improve the readability, because you can distinguish the number 1 and the char vector '1'.
You assumption that "a{1} has 7 characters", is not correct.
a{1} = '0.05*';
size(a{1})
>> [1, 5]
  댓글 수: 2
gsourop
gsourop 2018년 10월 26일
Hi Jan,
Thank you for answering my question. Nevertheless, if someones pastes the results in a word or notepad, the pasted value includes the quotes. I would like to figure out if there is a way to remove them automatically rather than removing them by hand.
Stephen23
Stephen23 2018년 10월 26일
편집: Stephen23 2018년 10월 26일
"I would like to figure out if there is a way to remove them automatically rather than removing them by hand."
Not really, because you would have to write an entire language parser to figure out which single quotes are required and which ones are just an artifact from displaying the character vectors.
"But doesn't seem to be the solution."
I have never seen any well written code that requires copy-and-pasting data. Why not just save the data properly using MATLAB data import/export tools? However, if you really want to copy-and-paste, then just print the char vectors using fprintf.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by