how to make a function which write a binary file in matlab
Show older comments
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.
- 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.
- 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).
- 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.
- Item four The function’s output argument is of type logical
- 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
Titus Edelhofer
on 8 Jun 2015
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 ...
Muhammad Usman Saleem
on 9 Jun 2015
Titus Edelhofer
on 9 Jun 2015
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
Thomas Koelen
on 9 Jun 2015
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.
Walter Roberson
on 9 Jun 2015
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
Guillaume
on 9 Jun 2015
@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
Walter Roberson
on 9 Jun 2015
You are correct, I guess I misremembered.
Muhammad Usman Saleem
on 10 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.
Accepted Answer
More Answers (1)
Walter Roberson
on 10 Jun 2015
0 votes
[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
Muhammad Usman Saleem
on 10 Jun 2015
Walter Roberson
on 10 Jun 2015
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?
Muhammad Usman Saleem
on 11 Jun 2015
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?
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!