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

49 ビュー (過去 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 Center および File ExchangeLow-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