Finding file in a before selected folder

4 Ansichten (letzte 30 Tage)
Debbie Oomen
Debbie Oomen am 13 Apr. 2018
Kommentiert: Stephen23 am 16 Apr. 2018
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 Kommentare
Rik
Rik am 16 Apr. 2018
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 am 16 Apr. 2018
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 13 Apr. 2018
Bearbeitet: Stephen23 am 13 Apr. 2018
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 Kommentare
Debbie Oomen
Debbie Oomen am 16 Apr. 2018
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?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Help and Support finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by