Ruut
Ruut

Reputation: 1161

concat 2 dimensional cell array with strings to one large string in matlab

I have the folliwing cell are of strings:

myCellArray = {'M1','M36','M129'} 

I would like to display the contents of myCellArray in an error message to the user of my function. If I simply do:

error(['Please correct elements with IDs:' cell2mat(myCellArray)])

The error looks like: Please correct elements with IDs:M1M36M129

How can I adjust the code that the elements are separated by a comma, so the error shown would be: Please correct elements with IDs:M1, M36, M129.

I tried something like:

a=num2cell({ ...
  myCellArray; ...
  repmat( ...
    {', '}, ...
    1, ...
    length(myCellArray) ...
  ) ...
});
b=strcat(a(:));

But this did not work.

Upvotes: 1

Views: 64

Answers (1)

Shai
Shai

Reputation: 114806

try using sprintf

>> error(['please correct elements with IDs: ', sprintf('%s, ', myCellArray{:}) ] )

results with

??? please correct elements with IDs: M1, M36, M129,

Upvotes: 1

Related Questions