I'm getting an error and don't know how to fix it.

조회 수: 1 (최근 30일)
Rebekah Higgins
Rebekah Higgins 2021년 5월 13일
댓글: David Fletcher 2021년 5월 13일
So I'm averaging a bunch of data and instead of opening up all the files manually and such, I'm using a for loop. Here's what my code looks like.
close all;
clc;
for k=1:10
filename=sprintf('20.5in_%d.bin',k);
fid=fopen('filename','r');
data=fread(fid,[1,Inf],'single');
sum=sum+data;
end
Average=sum/10;
plot(data/max(data));
plot(Average/max(Average));
close all
However, I get this error when I try to run it.
Error using fread
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in in205 (line 6)
data=fread(fid,[1,Inf],'single');
I'm not sure what the problem is or how to fix it. If I don't use a loop it works so any help would be greatly appreciated!!!
close all;
clc;
fid=fopen('20.5in_1.bin','r');
data=fread(fid,[1,Inf],'single');
plot(data/max(data));
close all
^the code that works without the loop

채택된 답변

David Fletcher
David Fletcher 2021년 5월 13일
The filename is the name held in the variable, the file isn't called 'filename'
fid=fopen(filename,'r')
  댓글 수: 2
Rebekah Higgins
Rebekah Higgins 2021년 5월 13일
Thanks!!! That's such a simple mistake! I can't believe I didn't catch that!
David Fletcher
David Fletcher 2021년 5월 13일
Easily done. I swear sometimes the brain sees what it thinks it should be seeing, and not what is actually there

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by