Finding file in a before selected folder

조회 수: 8 (최근 30일)
Debbie Oomen
Debbie Oomen 2018년 4월 13일
댓글: Stephen23 2018년 4월 16일
Hello everyone! I am new to GUI in MATLAB and I need to write an analysis user interface. I am just starting out and the first step for the GUI is to let the user select the directory that contains the data files. This is the code so far:
% % --- Executes on button press in ChooseFolder.
function ChooseFolder_Callback(hObject, eventdata, handles)
% hObject handle to ChooseFolder (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
folder = uigetdir('Search Folder Containing Data Files');
if isequal (folder, 0);
set(handles.DisplayFolderName, 'String', 'No folder selected');
set(handles.FileInputString, 'enable', 'off');
else
[~, name] = fileparts(folder);
textLabel = sprintf('Selected folder is %s', name);
set(handles.DisplayFolderName, 'String', textLabel);
filePattern = fullfile(folder, '*.csv');
allfiles = dir(filePattern);
for k = 1 : length(allfiles);
baseFileName = allfiles(k).name;
end
end
Now I want to add a Edit Text and Push Button. The Edit Text allows the user to enter the filename (for example): ID0123. Then, the Push Button can be pressed and MATLAB will look for this file in the before chosen directory. I have tried this:
% --- Executes on button press in findfile1.
function findfile1_Callback(hObject, eventdata, handles)
% hObject handle to findfile1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
filename = get(handles.edit1, 'String');
if exist (name, filename)
csvread(filename);
else
warningMessage = sprintf ('Warning: file does not exist:\n%s' , filename);
uiwait(msgbox(warningMessage));
end
But this does not do anything. Can anyone please help me with this issue?
Would be very much appreciated
  댓글 수: 2
Rik
Rik 2018년 4월 16일
Another probable cause of problems is that the ChooseFolder callback doesn't seem to store all information in the guidata handle struct.
And why did you close this question?
Debbie Oomen
Debbie Oomen 2018년 4월 16일
How can I store the information in the guidata handle struct?
I honestly did not know what it did. I thought nobody was reading this question anymore so that is why I closed it and posted it again. My mistake.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2018년 4월 13일
편집: Stephen23 2018년 4월 13일
I looked at the exist help, but could not find any syntax that matches how you are using it. Probably you should read the documentation and use one of the supported syntaxes, e.g.:
if 2==exist(filename,'file')
Also the line:
csvread(filename);
reads some data and then discards it immediately.
  댓글 수: 21
Debbie Oomen
Debbie Oomen 2018년 4월 16일
Okay, but I have found some information on using dir(*.*) for finding a file with a specific letter or letters in it. That way, the user could just type in the first few letters and number of the file and matlab finds it in the folder. I just cannot seem to figure out where it belong in my GUI code and if it would even work?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Help and Support에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by