fread() help. "Error using fread: Invalid file identifier..."
조회 수: 17 (최근 30일)
이전 댓글 표시
Hello everyone,
I am currently working in analyzing a set of data in the PTU format using a script called "Read_PTU_V1.m". However, I had an issue while running it. After the program opened a dialog box to select a file from my PC, I ran into the following error.
Error using fread
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in Read_PTU_V1 (line 57)
Magic = fread(fid, 8, '*char');
I have attached the section that I believe will present a good idea on what is the problem I have. It starts in line 53 and goes down to line 60.
% start Main program
[filename, pathname]=uigetfile('*.ptu', 'T-Mode data:'); %Opens dialog box to manually select the file using file explorer
fid=fopen(filepath);
Magic = fread(fid, 8, '*char');
if not(strcmp(Magic(Magic~=0)','PQTTTR'))
error('Magic invalid, this is not an PTU file.');
end;
I didn't write this script and honestly have no idea how to make it work. Please help a poor man with no matlab experience. I have attached the file as an extra resource.
댓글 수: 0
채택된 답변
dpb
2019년 6월 28일
[filename, pathname]=uigetfile('*.ptu', 'T-Mode data:'); %Opens dialog box to manually select the file using file explorer
fid=fopen(filepath);
Well, no, this could never have worked as written...the script returns the file and path in variables filename and pathname but then uses something entirely different to try open a file. That makes no sense whatsoever.
You'll have much better chance with something like
[filename, pathname]=uigetfile('*.ptu', 'T-Mode data:'); %Opens dialog box to manually select the file using file explorer
fid=fopen(fullfile(pathname,filename),'r');
You should then by all rights check for fid<0 and show any error messages if so...there's examples in the doc for fopen for such.
Doesn't engender much confidence in the rest of the script with such a horrible problem right off the bat...presuming it's as was distributed and not somehow butchered by other than the author.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 String Parsing에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!