reading a binary file into matlab

조회 수: 7 (최근 30일)
Abdul Wahab
Abdul Wahab 2018년 5월 22일
댓글: Abdul Wahab 2018년 5월 22일
I am ruining the following to codes
filelist = dir('E:\4_models\model1');
nt = size(filelist,1);
nx = 508;
ny = 637;
dx = 50;
dy = 50;
zdata = zeros(ny,nx,nt);
for i = nt
fname = filelist(i).name;
a = readcartesiangrid(fname);
contourf(a.x, a.y, a.z, 31);
colorbar
daspect([1 1 1]);
pause(0.1)
z = a.z;
zdata(:,:,i) = z;
end
for i = 2:nt
z1 = zdata(:,:,1);
zi = zdata(:,:,i);
zd = zi - z1;
imagesc(zd);
colorbar
set(gca,'Ydir','normal')
caxis([0 35]);
axis('equal')
pause(0.2)
end
function [ grid ] = readcartesiangrid(fname, nx, ny, dx, dy )
fid = fopen('fname');
version = fread(fid, 1, 'uint32');
assert(version == 2, sprintf('Invalid file version: expected 2, got %i', version));
fclose(fid);
when I execute these two codes, I am getting the following error;
Invalid file identifier. Use fopen to generate a valid file identifier
Error in readcartesiangrid (line 17)
version = fread(fid, 1, 'uint32');

채택된 답변

James Tursa
James Tursa 2018년 5월 22일
This line
fid = fopen('fname');
trys to open a file that is named exactly 'fname', which is not what you want. You probably want this:
fid = fopen(fname);
  댓글 수: 1
Abdul Wahab
Abdul Wahab 2018년 5월 22일
Thanks a lot.This helped!!

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

추가 답변 (1개)

OCDER
OCDER 2018년 5월 22일
편집: OCDER 2018년 5월 22일
I think there're other issues despite fixing fid = fopen(fname)
filelist = dir('E:\4_models\model1');
filelist = filelist(~[filelist.isdir]); %You have a /. and /.. folder that needs to be skipped
nt = size(filelist,1); %this gives you 1 number.
...
for i = nt %this will only do the last file. Do you want " for i = 1:nt " instead?
fname = filelist(i).name; %this gives you only the file name, without the file path (ex: model2.mat)
fname = fullfile(filelist(i).folder, filelist(i).name); %this gives you the full file path (ex: E:\4_models\model1\model2.mat)
...
end
In your readcartesiangrid, recommend the following changes:
fid = fopen(fname);
assert(fid ~= -1, 'Error opening the file "%s".', fname) %Add check here to help with debugging
  댓글 수: 1
Abdul Wahab
Abdul Wahab 2018년 5월 22일
This helped as well. Thank you for the detailed comment!!!

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

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by