fread() help. "Error using fread: Invalid file identifier..."
16 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
dpb
on 28 Jun 2019
[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.
More Answers (0)
See Also
Categories
Find more on String Parsing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!