Error in reading a dat file

4 次查看(过去 30 天)
Millone
Millone 2015-6-3
I am trying to read a binary file that was written as following:
if success== true
[row,col,v] = find(A);
row = uint32(row);
col = uint32(col);
fwrite(fid,size(A),'uint32');
fwrite(fid,nnz(A),'uint32');
for i = 1:size(v,1)
fwrite(fid, row(i), 'uint32');
fwrite(fid, col(i), 'uint32');
fwrite(fid, v(i), 'double');
end
end
using:
n = fread(fid,1,'double')
dims = fread(fid,n,'double')
A = fread(fid,'double')
A = reshape(A,dims')
fclose(fid);
but I get an error: Error using reshape Size vector must have at least two elements. Error in sparse(line 10) A = reshape(A,dims')
How can I solve this problem? Any help will be appreciated. Thanks

回答(1 个)

James Tursa
James Tursa 2015-6-3
编辑:James Tursa 2015-6-3
How is fread supposed to know that you wrote uint32 values to the file unless you tell it? Read in the uint32 values as uint32, not double.
EDIT:
Maybe something like this (CAVEAT: I am not on a machine with MATLAB at the moment so this is untested)
size_A = fread(fid,[1 2],'uint32');
nnz_A = fread(fid,[1 1],'uint32');
row = zeros(nnz_A,1,'uint32');
col = zeros(nnz_A,1,'uint32');
v = zeros(nnz_A,1);
for i = 1:nnz_A
row(i) = fread(fid, [1 1], '*uint32');
col(i) = fread(fid, [1 1], '*uint32');
v(i) = fread(fid, [1 1], 'double');
end
Then rebuild A from the pieces.
  1 个评论
Millone
Millone 2015-6-3
Thanks for your comment. It is progressing but now, after I changed to uint32 I have a new error. Error using reshape To RESHAPE the number of elements must not change.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by