How can I strip single quotes from a cell array?

6 views (last 30 days)
gsourop
gsourop on 26 Oct 2018
Edited: Stephen23 on 26 Oct 2018
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 Comment
Stephen23
Stephen23 on 26 Oct 2018
Edited: Stephen23 on 26 Oct 2018
"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}

Sign in to comment.

Answers (1)

Jan
Jan on 26 Oct 2018
Edited: Jan on 26 Oct 2018
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 Comments
gsourop
gsourop on 26 Oct 2018
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 on 26 Oct 2018
Edited: Stephen23 on 26 Oct 2018
"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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!