Error in reading a dat file
조회 수: 4 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (1개)
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!