fread() help. "Error using fread: Invalid file identifier..."

조회 수: 17 (최근 30일)
Jose Godinez
Jose Godinez 2019년 6월 27일
댓글: Jose Godinez 2019년 7월 1일
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.

채택된 답변

dpb
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.
  댓글 수: 1
Jose Godinez
Jose Godinez 2019년 7월 1일
Thank you so much. I wish I have never been given the task to make this work!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by