Importing text files using uigetfile

조회 수: 5 (최근 30일)
Gavin Brown
Gavin Brown 2015년 6월 22일
편집: Stephen23 2015년 6월 23일
I'm attempting to import some data from a text file. When I use the Import Data tool and then use the generate script function it works fine. (snippet below)
filename = 'C:\Documents and Settings\gavinbr\Desktop\Muirake\test files\NL_001_OCT_Lp _0001_0001.rnd';
delimiter = ',';
startRow = 3;
formatSpec = '%*s%*s%*s%*s%*s%*s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'HeaderLines' ,startRow-1, 'ReturnOnError', false);
However I wish to replace the first line with:
[filename,pathname] = uigetfile('*.rnd','Select the rnd file to process','Multiselect','on');
So I can select the files I wish to process each time and select more than one file each time. However it returns the following error:
Error using textscan Invalid file identifier. Use fopen to generate a valid file identifier.
Error in importscript (line 33)
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'HeaderLines' ,startRow-1, 'ReturnOnError', false);
Does anyone know how I can get around this?
Thanks in advance

답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2015년 6월 22일
Check the value of fileID
fileID = fopen(filename,'r')
If fileID is equal to -1, that means you can't open the file for many reasons, maybe the file doesn't exist.
  댓글 수: 5
Gavin Brown
Gavin Brown 2015년 6월 23일
When I use that it returns with "No such file or directory". I don't really understand how it can't exist if I selected it. I'm sort of new to this. Will it have something to do with the pathname?
Cheers
Stephen23
Stephen23 2015년 6월 23일
편집: Stephen23 2015년 6월 23일
Probably you are only providing the filename to fopen, and not the path as well. If you do not supply any path information, then fopen will only look in the current directory. Consider the difference:
fid = fopen('my_work.txt','rt');
looks for a file called my_work.txt located in the current directory. Whereas
strP = 'C:\Users\Anna\Results';
strF = 'my_work.txt';
fid = fopen(fullfile(strP,strF),'rt');
also provides the directory location. You should look at Jan Simon's answer, which shows you how to use this in a complete example, but basciyll you need to do this:
[filename,pathname] = uigetfile(...);
str = fullfile(pathname,filename);
fid = fopen(str,'rt');
... ETC

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


Jan
Jan 2015년 6월 23일
편집: Jan 2015년 6월 23일
[filename, pathname] = uigetfile('*.rnd', 'Select the rnd file to process', 'Multiselect', 'on');
if isequal(filename, 0)
disp('User aborted reading of files.');
return;
end
for k = 1:length(filename)
aFile = fullfile(pathname, filename{k});
% Now perform the operation with this file. E.g. show its name:
disp(aFile);
fid = fopen(aFile, 'r');
if fid < 0
error('Cannot open file: %s', filename);
end
...
end

카테고리

Help CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by