MATLAB: Reading a binary file into matlab

fread

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');

Best Answer

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);