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

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

I do not sure whether step 1 is correct done by me or not.
You'll know straight away as soon as you test your code, as you've told numerous times.
Reading the documentation of fwrite wouldn't hurt either.
Hmm, I think Guillaume has indeed the right to comment. Especially when there is someone asking the community to do his homework day after day ...
There is not assistance to this question till yet>....... :/
Some hints:
fwrite(fileID,A)
fwrite(fileID,A,precision)
fwrite(fileID,A,precision,skip)
fwrite(fileID,A,precision,skip,machinefmt)
So you need to replace
fwrite(fileid,uint32(row),uint32(col),unit32(n));
with
fwrite(fileid, uint32([row col n]), 'uint32');
fwrite(fileid, [row col n], 'uint32');
where both should work equally well ([row col n] would be the "A" from the help).
Then continue exporting the other data as written in the items. Use find to get the non zero entries (rows, columns, values).
Titus
You'll never learn if you keep asking the community EVERYTHING you can't figure out yourself. The goal of the Matlab course you are following is to understand and write code in Matlab, not to extend your social skills on a user forum.
fwrite defaults to writing in the same precision as the data type of the values passed in, so
fwrite(fid, uint32(row))
is enough without needing 'uint32' passed as an option
@Walter, in 2015b, if you don't specify the precision, fwrite writes everything as uint8 regardless of the data type.
>>fid = fopen('test.txt', 'w');
>>fwrite(fid, uint32('Hello World'));
>>fclose(fid);
>>fid = fopen('test.txt', 'r');
>>fread(fid, 'uin32')'
ans =
1819043144 1867980911
You are correct, I guess I misremembered.
@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..
@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

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)

[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 thanks for your kind contributions.. With your assistance i have make that but error is same
function sparse_array_out(A,filename)
fid=fopen(filename,'w+')
[row col]=size(A);
n = nnz(A);
fwrite(fileid, uint32([row col n]), 'uint32');
[row,col,v] = find(A)
row1=uint32(row);
col1=uint32(col);
m=[row1,col1,n];
fclose(fileID);
end
??
What error is the same?
Did you notice that you do not write m to the file?
Did you notice that your code does not comply with step 4 or step 5?
@Walter, He means that he gets the same answer from the grader which is only telling him that his solution is not correct for some given input.
no this time i am testing it manually...
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.

Categories

Products

Community Treasure Hunt

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

Start Hunting!