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

1 回表示 (過去 30 日間)
Mahdi
Mahdi 2015 年 3 月 5 日
コメント済み: Mahdi 2015 年 3 月 6 日
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
  1 件のコメント
Mahdi
Mahdi 2015 年 3 月 6 日
it works! thank you very much1

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by