Needs help in uploading only the needed file to the user interface

I am working on a project where i am having 3 data sets say a,b,c which is saved in the .csv format. In the user interface i want to upload those files,for which i am having 3 browse buttons in separate sections.So how can i allow the user to select only the intended file using the browse button. Means i want the user to select only the file a.csv in the section for a using the particular browse button and b.csv in the section for b with the corresponding browse button. Is it possible to do so? All the data sets are saved in the same folder.

 Accepted Answer

This is easy: simply tell uigetfile exactly which filenames you want the user to choose from, and it will only let them select from those exact files. This is much simpler than letting the user select any CSV file and then doing some awkward checking afterwards.
files = {'A.csv';'B.csv';'C.csv'}; % the permitted filenames
str = sprintf(';%s',files{:});
[fName,fPath,idx] = uigetfile({str(2:end),'CSV file'})
Remember to check if the user closed the dialog box:
assert(idx>0,'User closed the dialog box!')

1 Comment

Thank you so much sir. This is what i was looking for.It worked perfectly fine.

Sign in to comment.

More Answers (1)

When the directory is decided, use dir() to find the names of potential files. Set the String property of either a uicontrol('Style','popup') or 'style' 'listbox' to create the lists of files available to select from. When the user has made their selection, the Value property of the uicontrol will be set the the index of the name used. If you still have the list of files you could index into that; if not then you can pull out the String property of the uicontrol and index into it.

5 Comments

Actually i am a beginner. So can you be more specific on what to do next?
[file,path] = uigetfile('*.csv;','Choose the data file');
i only want to check whether the user selected the needed data set or not. Does the checking using file name is possible?
Please do not use a variable named path as that interferes with using the MATLAB path.
We cannot answer your question yet, because we do not know which is the "needed" data set.
If you need to know that the user has selected a file, then test
if ischar(file)
If the user closes the uigetfile() dialog, or clicks on 'Cancel' then the output of uigetfile is numeric 0 (not the character '0'). uigetfile() waits until the user selects a file or closes the dialog or cancels, so you do not need to do any kind of checking like
while user_has_not_made_a_uigetfile_choice_yet() %NOT NEEDED
pause(2);
end
You just
[file, pathname] = uigetfile('*.csv;','Choose the data file');
if ~ischar(file)
error('You did not choose a file. I do not know what to do. I quit.')
end
data = xlsread( fullfile(pathname, file) ); %read the file
Sir the needed data set in the sense ,in my project there are 3 sections. one for quality of work life(qwl), one for workplace empowerment(wem) and the 3rd one for emp commitment(ec). For each sections i have separate browse buttons. When i click browse to select the data set,the window that appears have 3 data sets namely qwl.csv,wem.csv and ec.csv as all the three data ssets are saved in the same folder. My actual question is - whenever i click wem.csv in the section for quality of worklife, user should get a alert like "choose the correct file".Please help me out.
If the file names are fixed then why ask the user to select a file name? At most you would need the user to select the directory, which you can do with uigetdir()

Sign in to comment.

Categories

Find more on App Building in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!