1

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.

1 Answer 1

1

try using sprintf

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

results with

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

Sign up to request clarification or add additional context in comments.

3 Comments

error('...%s%s',sprintf('%s, ',myCellArray{1:end-1}), sprintf('%s.', myCellArray{end})) to avoid last comma.
@OlegKomarov the last comman is annoying, but is it worth all the trouble for removing it?
Depends, and that's why I commented instead of answering :).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.