how to make a function which write a binary file in matlab

조회 수: 2 (최근 30일)
Muhammad Usman Saleem
Muhammad Usman Saleem 2015년 6월 8일
편집: Stephen23 2015년 6월 15일
Hi everyone; I am going to make a function sparse_array_out1 that takes two input arguments and returns one output argument. Its first argument is a two-dimensional array of doubles, which it writes into a binary file.Item one The name of that file is the second argument to the function.The file must have the following format.
  1. Item one It starts with three uint32 scalars specifying the number of rows of the array followed by the number of columns followed by the number of non-zero elements in the array.
  2. Item two Then each non-zero element of the array is represented by two uint32 scalars and a double scalar in the file in this order: its row index (uint32), its column index (uint32), and its value (double).
  3. Item three Note that this file format is an efficient way to store a so-called “sparse array”, which is by definition an array for which the overwhelming majority of its elements equal 0.
  4. Item four The function’s output argument is of type logical
  5. Item five and equals false if there was a problem opening the file and true otherwise.
I have no idea what i am doing but i have created that code for point no 1 and starting of function:
function sparse_array_out1(A,filename)
fid=fopen(filename,'w+')
[row col]=size(A);
n = nnz(A);
fwrite(fileid,uint32(row),uint32(col),unit32(n));
if fid=>0
fwrite(fid,A,double)
I do not sure whether step 1 is correct done by me or not... Totally do not know how to make this function for step 2,3,4,5 . Any assistance will be highly appreciable..
  댓글 수: 10
Muhammad Usman Saleem
Muhammad Usman Saleem 2015년 6월 10일
@Titus Edelhofer after writing that code , i am asking from you whether i have complete step 1 or not?
function sparse_array_out(A,filename)
fid=fopen(filename,'w+')
[row col]=size(A);
n = nnz(A);
fwrite(fileid, uint32([row col n]), 'uint32');
I want hint from you how to tackle step no 2 , to me i can use checks as i have mentioned in the above code but no idea will it work or not? hint me more easily..
Stephen23
Stephen23 2015년 6월 10일
편집: Stephen23 2015년 6월 15일
@Muhammad Usman Saleem: "i am asking from you whether i have complete step 1 or not"
The answer to this question is very simple: TEST YOUR CODE
This advice has been given to you many times now: TEST YOUR CODE.
Here are those three words again: TEST YOUR CODE.
Testing code does not mean using the automatic code grader from your course, nor does it mean asking random people on the internet if your code works properly. Testing your code means figuring out some test cases, running the code with those test cases, and checking that it performs like you expect it to.
Because this is actually what anyone you ask would do: They would take some code, use its specifications to predict how it should behave, run it according to its specifications, and see if it performs as expected. There is no reason why you cannot do this yourself, thus a) actually learning something about programming, and b) being much faster than asking random people on the internet.

댓글을 달려면 로그인하십시오.

채택된 답변

Marcos Mariano
Marcos Mariano 2015년 6월 13일
편집: Marcos Mariano 2015년 6월 13일
Maybe help you:
function output = sparse_array_out(A,data_name)
% The first argument(A) is a two two-dimensional array of doubles
% And the second argument is(data_name),it writes into a binary file
fid = fopen(data_name,'w+');
output = true; % Output argument is type logical - Inializate output
if fid < 0
output = false; % MSG error - If we have problem opening we need to change output for FALSE
return;
end
%// Filename needs to return off 3 unit32 scalars
[rows, cols] = size(A);
fwrite( fid,size(A,1),'uint32'); %%number of the rows
fprintf('Number of rows: %d %s\n', rows, class(rows));
fwrite( fid,size(A,2),'uint32'); %%number of the columns
fprintf('Number of cols: %d %s\n', cols,class(cols));
[r, c, v] = find(A); %%get non-zero element row, col ids and values
fwrite(fid,length(v),'uint32'); %%no of non-zero elements
fprintf('Number of non-zeros elements: %d %s\n', length(v), class(v));
for jj = 1:cols
for ii = 1:rows
id_row = ii;
id_col = jj;
id_value = A(ii, jj);
if (A(ii, jj))~=0
fwrite(fid, id_row,'uint32'); %%row id of non-zero element ii
fprintf('Index row: %d %s\n',id_row,class(id_row));
fwrite(fid, id_col,'uint32'); %%col id of non-zero element ii
fprintf('Index column: %d %s\n',id_col, class(id_col));
fwrite(fid, id_value,'double'); %%value of non-zero element ii
fprintf('Index value: %d %s\n',id_value, class(id_value));
end
end
end
fclose( fid );
end

추가 답변 (1개)

Walter Roberson
Walter Roberson 2015년 6월 10일
[row,col] = find(_) returns the row and column subscripts of each nonzero element in array X using any of the input arguments in previous syntaxes.
[row,col,v] = find(_) also returns vector v, which contains the nonzero elements of X.
  댓글 수: 5
Muhammad Usman Saleem
Muhammad Usman Saleem 2015년 6월 11일
no this time i am testing it manually...
Walter Roberson
Walter Roberson 2015년 6월 11일
What is the error message you are getting now? Or how are you determining that what you are doing is not what is expected?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by