Write a warning dialog with a dynamic number of inputs

5 Ansichten (letzte 30 Tage)
mlwermers
mlwermers am 21 Jul. 2021
Kommentiert: dpb am 21 Jul. 2021
Hello!
I would like to create a warning dialog for my script with a dynamic number of inputs to list how many folders in a scenario are empty.
For example, if I have 5 folders (folderA through folderE), and three of them are empty, the script should determine which three were empty and list them in the warning dialog such that the warning dialog says:
" The following folders are empty:
folderA
folderC
folderD"
I would like this to work no matter how many folders are empty. So if only two folders are empty, only two would be listed, and if all five folders are empty, all five would be listed.
I have the list of empty folders saved in a cell array.
I initially tried running the warning message through a for loop, but it overwrote the message each time and only one folder was listed at the end.
I also tried the following code:
warningmessage = sprintf('The following folders are empty:\n%s\n',char(emptyFolders));
% empty folders is a cell array with a variable number of folder names.
warndlg = (warningmessage,'Warning');
The result from this code (for three empty folders) is a warning dialog with the message:
"The following folders are empty:
fffooollldddeeerrrACD"
Is there a way to write a variable number of strings into a message?
Thanks!

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 21 Jul. 2021
warningmessage = sprintf('The following folders are empty:\n%s\n', strjoin(emptyFolders, '\n'));
This assumes that emptyFolders is cell array of character vectors or string() array (and not 2d character array)

Weitere Antworten (1)

dpb
dpb am 21 Jul. 2021
Two ways -- for your above with cellstr() array, just use {:} instead of char() ...
>> emptyF=cellstr("Folder"+['A':'C'].')
emptyF =
3×1 cell array
{'FolderA'}
{'FolderB'}
{'FolderC'}
>>
>> sprintf('%s\n',emptyF{:})
ans =
'FolderA
FolderB
FolderC
'
>>
I had created as string array first and sprintf is now strings enabled...
>> emptyF="Folder"+['A':'C'].'
emptyF =
3×1 string array
"FolderA"
"FolderB"
"FolderC"
>> sprintf('%s\n',emptyF)
ans =
'FolderA
FolderB
FolderC
'
>>
You can, if you don't want the extra \n at the end count the size of the array and build an array-size dependent format string by
fmt=[repmat('%s\n',1,numel(emptyF)-1) '%s'];
Of course all these need to be prefixed with the fixed string to write as the first portion of the format expression...

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by