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

1 次查看(过去 30 天)
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?!

采纳的回答

Guillaume
Guillaume 2015-3-5
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by