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

1 visualización (últimos 30 días)
Mahdi
Mahdi el 5 de Mzo. de 2015
Comentada: Mahdi el 6 de Mzo. de 2015
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?!

Respuesta aceptada

Guillaume
Guillaume el 5 de Mzo. de 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

Más respuestas (0)

Categorías

Más información sobre Environment and Settings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by