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!!!

サインインしてコメントする。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by