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

1 view (last 30 days)
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 Comments
Muhammad Usman Saleem
Muhammad Usman Saleem on 10 Jun 2015
@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 on 10 Jun 2015
Edited: Stephen23 on 15 Jun 2015
@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.

Sign in to comment.

Accepted Answer

Marcos Mariano
Marcos Mariano on 13 Jun 2015
Edited: Marcos Mariano on 13 Jun 2015
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

More Answers (1)

Walter Roberson
Walter Roberson on 10 Jun 2015
[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 Comments
Walter Roberson
Walter Roberson on 11 Jun 2015
What is the error message you are getting now? Or how are you determining that what you are doing is not what is expected?

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!