How to implement the fwrite function as in C language?

Hi there, I want to know if it is possible to implement the fwrite function same as the C language fwrite.
To further explain my question let's say we have fwrite function in C which writes binary data into a file. The syntax of the function is as follow
fwrite(memory containing the data, number of bytes each item to be written, total number of items to write, destination file)
example
fwrite(iq_buff, 2, 2*2600000, signal.bin)
Now my question is how can we specify the number of bytes to be written during the fwrite function and how much data needs to be written in the file?

3 Comments

Matlab will write the entire variable (although you have the option to skip bytes from the start). You will have to index your variable to skip trailing bytes.
The Matlab fwrite function does not provide the exact syntax you describe, but you should be able to write a wrapper that does. What did you try so far?
If all else fails, you can also use mex to actually use the C function.
can you provide me some guidance on MEX to use the C function? I have tried using the following command till now
fid = fopen('gps_sig.bin','ab');
fwrite(fid,'int8');
fclose(fid);
@Rik If I want to create a wrapper function to get the exact same output what would I need to do? Can you please provide me with any guidance or reference?
I am stuck in specifying the number of elements and bytes to be written

Sign in to comment.

 Accepted Answer

"I want to know if it is possible to implement the fwrite function same as the C language fwrite."
No, it is not possible in MATLAB.
Your prototype for fwrite is incorrect.
First parameter is the memory location to copy out. In MATLAB that would be the name of the variable, or an expression whose value was to be written out.
Second parameter is the size of one item. In MATLAB that could be the number of bytes in the variable, as determined using whos()
Third parameter is the count. In MATLAB that could be 1.
The fourth parameter to C's fwrite is a pointer to a FILE structure. MATLAB does not have FILE structure, and does not offer pointers (in most contexts). Using the name of a file would not be compatible with C.

10 Comments

C itself provides only buffered I/O with FILE*, which are never numeric. MATLAB's I/O uses the function names of the C buffered I/O functions, but the interface available to the user is purely in terms of numeric file identifiers, similar to an unbuffered I/O interface such as POSIX offers.
MATLAB's fwrite is different from C's in an incompatible way that cannot be overcome within MATLAB: In C, if you fseek() past the end of the file and then write data, then logical zeroes are inserted between the previous end of file and the write point. The operating system has the option to record the size of the gap and not put real data on disk there. In MATLAB, it is not possible to fseek past the end of file.
Thank you for so much detailed answers. Ok if I want to create a wrapper for the following code snipped what are the things that I will need?
fwrite(iq_buff, 2, 2*iq_buff_size, fp);
What is your aim here? Is your aim to generate C code that uses C fwrite() ? MATLAB Coder already handles translating MATLAB's fwrite() to C.
The goal is to write IF data generated and stored in the iq_buff variable into a gps.bin file same as the above example from the C code
If you are asking what the C code is that is equivalent to
%MATLAB
fid = fopen('gps.bin', 'w');
fwrite(fid, iq_buff)
fclose(fid)
then it is
fp = fopen('gps.bin', 'wb');
fwrite(iq_buff, sizeof(iq_buff), 1, fp) %C
fclose(fp)
Is that a matlab executable code?
Sir the folliwing code is not working... sizeof is not a recognized function or variable
fp = fopen('gps.bin', 'wb');
fwrite(iq_buff, sizeof(iq_buff), 1, fp) %C
fclose(fp)
I want to replicate the following function in matlab
fwrite(iq_buff, 2, 2*iq_buff_size, fp);
The iq_buff_size is 26,00000
If you insist.
function status = FWRITE(data, size_per_element, number_of_elements, fp)
temp = typecast(data, 'uint8');
if size_per_element * number_of_elements > numel(temp)
error('Requested to write more data than exists in the input')
end
for offset = 1 : size_per_element : number_of_elements * size_per_element
fwrite(fp, temp(offset:offset+size_per_element-1), 'uint8');
end
end
But wouldn't it be a lot easier to just
fwrite(fp, data, 'uint16')
or whatever the appropriate data type is?
Specifying a size of 2 and a large count is nearly the least efficient way to fwrite (writing one byte at a time being worse.) It is more efficient to write in multiples of the physical page size (which is typically 4 Kb)
Thank you so much Robert, Actually I am working on a GPS simulator and have generated the Intermediate frequency data but when I use the fwrite function, it writes a very little amount of data which is not enough for the tracking and navigation solution extraction. That's why I wanted to implement the exact code as in the C code hoping that I will get the right amount of data.

Sign in to comment.

More Answers (0)

Products

Release

R2019b

Community Treasure Hunt

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

Start Hunting!