Error in reading a dat file

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 日

0 投票

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.

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

カテゴリ

ヘルプ センター および File ExchangePerformance and Memory についてさらに検索

タグ

質問済み:

2015 年 6 月 3 日

編集済み:

2015 年 6 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by