Cody Problem 41. Cell joiner, not sure why spaces aren't showing

2 views (last 30 days)
I'm working on Cody Pr 41 and I think I had a solution
function out_str = cellstr_joiner(in_cell, delim)
out_str = strcat(in_cell(1))
for i = 2:length(in_cell)
out_str = strcat(out_str, delim , in_cell(i))
end
end
but the delim isn't showing when it's equal to '', but it will show if its equal to '?'
Any ideas why?

Answers (2)

the cyclist
the cyclist on 31 May 2016
Edited: the cyclist on 31 May 2016
The code
''
will not be a space. It will be null. The code
' '
will be a space. Does that help?

the cyclist
the cyclist on 2 Jun 2016
Edited: the cyclist on 2 Jun 2016
So, I peeked into the code for strcat, and see these lines
if ~isempty(str) && (str(end) == 0 || isspace(str(end)))
str = char(deblank(str));
end
(in lines 82-84) of the code.
So, the code is "deblanking", even when you are explicitly trying to append a white space.
This was certainly not obvious to me in the documentation, but it is definitely what the strcat code is intending to do.
  2 Comments
Steven Lord
Steven Lord on 2 Jun 2016
See the third paragraph in the Description section of the documentation for strcat. If you're using release R2013a or later, consider using strjoin instead.
the cyclist
the cyclist on 2 Jun 2016
Hey, Steven. I did read and mull the documentation prior to my post. I don't really think that paragraph describes his situation.
First, he is inputting cell arrays, not character arrays, so the paragraph you reference isn't relevant.
Second, this is not exactly "trailing" whitespace, because he is doing a concatenation of the form
strcat('s1',' ','s2')
So, I agree with you that I can retroactively understand the documentation for his situation, but I don't think I would have anticipated this behavior.

Sign in to comment.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!