Returning both chosen and not chosen elements

Hello, I currently have a popup list where I can choose the filenames of files that I want to manually do something with. I am able to use the indices to returned from listdlg to find the names of the files that I want to work with, however; I need the output to also include the NaN's for the files that I did not choose. For example if I chose the first and third element in the list I would want manual_val = ['name of file 1' 'NaN' 'name of file 2']. This is what I have so far!
%% Select folder with all the data in it
if exist('dataDir')~=1
dataDir = uigetdir(pwd);
end
%looks for any files ending in .out
dataFiles = dir([dataDir, '/*.csv']);
%get all file names from the chosen folder
fname_total = [];
for k = 1:length(dataFiles)
fname = dataFiles(k).name;
fname_total = [fname_total; fname];
end
fname_total = cellstr(fname_total);
%Choose graphs for manual entry
manual = input('Are there any graphs you want to input manually? ');
if manual == 1
manual_idx = listdlg('PromptString', 'Choose Files to Analyze', ...
'ListString',fname_total,'SelectionMode','multiple');
manual_val = fname_total(manual_idx);
else
manual_val = {'none'};
end

 Accepted Answer

This will create a variable fname_out, which is a cell array whose length is the number of .csv files located in the specified directory and each element of which is a file name if that file was selected in the listdlg or 'NaN' if not.
(If you no longer need manual_val you can remove it or rename fname_out to manual_val, i.e., it wasn't clear whether what you want to do is meant to replace what you already have or to supplement it.)
%% Select folder with all the data in it
if exist('dataDir')~=1
dataDir = uigetdir(pwd);
end
% looks for any files ending in .csv
dataFiles = dir([dataDir, '/*.csv']);
fname_total = {dataFiles.name}; % this is easier than building a cell array of file names in a loop
%Choose graphs for manual entry
manual = input('Are there any graphs you want to input manually? ');
if manual == 1
manual_idx = listdlg('PromptString', 'Choose Files to Analyze', ...
'ListString',fname_total,'SelectionMode','multiple');
manual_val = fname_total(manual_idx);
else
manual_idx = [];
manual_val = {'none'};
end
fname_out = cell(size(fname_total));
fname_out(:) = {'NaN'};
fname_out(manual_idx) = fname_total(manual_idx);

More Answers (1)

Adapt this to suit your needs
list = {'Red','Yellow','Blue',...
'Green','Orange','Purple'};
[indx,tf] = listdlg('ListString',list);
selection = repmat("NaN",size(list));
selection(indx) = list(indx)
% RESULTS after selecting 2nd and 3rd options
selection =
1×6 string array
"NaN" "Yellow" "Blue" "NaN" "NaN" "NaN"

Categories

Find more on Update figure-Based Apps in Help Center and File Exchange

Asked:

on 16 Mar 2022

Answered:

on 16 Mar 2022

Community Treasure Hunt

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

Start Hunting!