reading a binary file into matlab

6 visualizaciones (últimos 30 días)
Abdul Wahab
Abdul Wahab el 22 de Mayo de 2018
Comentada: Abdul Wahab el 22 de Mayo de 2018
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');

Respuesta aceptada

James Tursa
James Tursa el 22 de Mayo de 2018
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);

Más respuestas (1)

OCDER
OCDER el 22 de Mayo de 2018
Editada: OCDER el 22 de Mayo de 2018
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 comentario
Abdul Wahab
Abdul Wahab el 22 de Mayo de 2018
This helped as well. Thank you for the detailed comment!!!

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by