Help with isempty and exists

Hello community,
I have a simple problem. I want to prompt the user to enter a directory name, check if it exist. If it does, store that fullpath into my guidedata. If it doesnt, create the directory and also 2 subdirectories within that directory then store fullpath into guide data. The name of the directory I want to search and create folders in is named 'Labelers.' It's fullpath is C:\Users\ecorbett\Documents\MATLAB\Labelers So far I have this code:
prompt = {'Enter Labeler Name:'};
dlg_title = 'Labeler Name';
num_lines = 1;
answer = inputdlg(prompt);
if isempty('Labelers'(answer))
mkdir('Labelers','answer')
else
handles.name = answer;
guidata(gcf, handles);
handles = guidata(gcf);
end
It's not working thus far. Any advice will be greatly appreciated!

2 个评论

Why don't you just call uigetdir to let the user specify a folder and create one if needed? The "New folder" button is right there on the dialog box.
Hmmm, hadn't considered that option. I suppose that would have been easier.

请先登录,再进行评论。

 采纳的回答

root = 'C:\Users\Oleg\Desktop\';
answer = inputdlg(prompt);
if isempty(answer{1}) || exist([root answer{1}],'dir') ~= 7
mkdir(root,'answer')
else
handles.name = answer;
guidata(gcf, handles);
handles = guidata(gcf);
end

12 个评论

if exist(answer,'C:\Users\ecorbett\Documents\MATLAB\Labelers') ~= 7
mkdir('answer')
I get this error:
??? Undefined function or method 'exist' for input arguments of type 'cell'.
Error in ==> act_ive_gui_new>act_ive_gui_new_OpeningFcn at 90
if exist(answer,'C:\Users\ecorbett\Documents\MATLAB\Labelers') ~= 7
I guess its because 'answer' is a cell array and exist doesnt support that type of input.
if exist(answer{1},'dir')
Don't change 'dir'!
Great that works! I have one more question:
mkdir('C:\Users\ecorbett\Documents\MATLAB\Labelers','answer')
Right answer as the filename instead of the variable within answer. Do I need to remove the quotes?
Execute in the command line:
answer{1}
'answer'
And verify what's in ans.
I see, the {1} is used to index the cell array?
Yes, you get to access the content of a cell array with c{} while c() retrieves the content wrapped in the cell.
I see. So if I wanted to make a subdirectory within that newly created directory I would use:
mkdir('../answer{1}','newFolder')
?
Not quite.
mkdir(['../' answer{1} '/newFolder'])
Or more readable perhaps,
mkdir(sprintf('../%s/newFolder', answer{1}))
Thats funny, neither of them seem to work?
if exist(answer{1},'dir')~= 7
%the line below works, but the lines to create the subfolders dont
mkdir('C:\Users\ecorbett\Documents\MATLAB\Labelers',answer{1})
mkdir(['../' answer{1} '/Labels'])
mkdir(['../' answer{1} '/Segmentation'])
Nevermind Walter! That works! I had something else wrong! Thank you

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Environment and Settings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by