Clear Filters
Clear Filters

How show variable number of cell's array in a message box?

1 view (last 30 days)
Hi everyone,
I want a cell, which includes every time (depend on calculation) different number of array. It means, depend on calculation the number of arrays could be 2 or 3 etc. Now I want to show the content of this cell in a message box. I have written the following:
%X_AP_Name is the cell with variable number of arrays
msgbox(sprintf('Please insert *.txt-data of %s in folder sample',X_AP_Name{:}))
but the text before and after arrays of cell is repeated according to number of arrays. If for example Size(X_AP_Name)=2, it result in following text: "Please insert *.txt-data of A in folder samplePlease insert *.txt-data of B in folder sample"
But i want to have "Please insert *.txt-data of A and B in folder sample"
what should I do?!

Accepted Answer

Guillaume
Guillaume on 5 Mar 2015
A simple way is to use strjoin on your cell array of strings:
msgbox(sprintf('Please insert *.txt-data of %s in folder sample', strjoin(AP_Name, ' and ')));
You could make something a bit more advanced to make the sentence look nicer, like:
function filestring = buildfilestring(filenames)
%filenames: a cell array of string
if numel(filenames) > 2
filestring = sprintf('%s and %s', strjoin(filenames(1:end-1), ', '), filenames{end});
else
filestring = strjoin(filenames, ' and ');
end
end

More Answers (0)

Categories

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

Community Treasure Hunt

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

Start Hunting!