Find if a folder already exists and act depending on the result!!!

15 次查看(过去 30 天)
I ve created a GUI that generates some folders where some files are saved.The folder takes the name that user gives it .When the user creates a folder with the same name Matlab generates a warning .
.I would like to ask if there is a way to use this warning ?For example if the user create the same folder and there is a warning show a message let the user decide where to save the data and if not continue !! I ve tried to produce this kind of code (catch the warning) but with no results(i am afraid i am doing it wrong) ,so if someone has another idea on how I can check the name of the folders and just inform the user that has put the same name in the folder and then act i will be covered!!
Here is the code ...the user press the pushbutton16. and depending on some radiobutton choices that he made creates the folder .So if the name of the folder has been use again a warning is generated in the line after mkdir : Warning: Directory already exists. (handles.Fak_asthn is the name of the folder )
% --- Executes on button press in pushbutton16.
function pushbutton16_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton16 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
h_selectedRadioButton = get(handles.uipanel21,'SelectedObject');
selectedRadioTag = get(h_selectedRadioButton,'tag');
switch selectedRadioTag
case 'radiobutton2'
mkdir('DWI_Liver_Analyzer/PRIMARY PATIENTS',handles.Fak_asthn)
Thanks in advance!!!

采纳的回答

Walter Roberson
Walter Roberson 2012-5-25
fn = fullfile('DWI_Liver_Analyzer/PRIMARY PATIENTS', handles.Fak_asthn);
if exist(fn, 'dir')
warning(....)
else
mkdir(fn)
end

更多回答(1 个)

Image Analyst
Image Analyst 2012-5-25
My code snippet here will help you turn off that warning:
% Turn off this warning "Warning: Image is too big to fit on screen; displaying at 33% "
% To set the warning state, you must first know the message identifier for the one warning you want to enable.
% Query the last warning to acquire the identifier. For example:
% warnStruct = warning('query', 'last');
% messageID = warnStruct.identifier
% messageID =
% MATLAB:concatenation:integerInteraction
warning('off', 'Images:initSize:adjustingMag');
  2 个评论
Paraskevas Papanikolaou
Thanks Image Analyst but i dont want just to turn off that warning ,i would like to inform the user if he is using a name for a folder that already exists!!Moreover the warning that Matlab generates doesnt have a msg_id. Perhaps i can turn off the warning and check the names of the folders with another way /another function?....ANY IDEAS?
Image Analyst
Image Analyst 2012-5-25
The just use exist(yourFolderName, 'dir') to see if the folder exists and warn the user that the folder exists already (though I wouldn't do that - I'd just continue on silently without telling the user):
if exist(yourFolderName, 'dir')
warningMessage = sprintf('The folder %s already exists!', yourFolderName);
uiwait(warndlg(warningMessage));
else
mkdir(yourFolderName);
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by