writing a buffer of 80 characters to file in a binary format

조회 수: 7 (최근 30일)
AP
AP 2013년 6월 14일
I am trying to translate the following snippet which is in C.
char buffer[80];
strcpy(buffer,"Velocity");
fwrite(buffer,sizeof(char),80,file_ptr);
Below is the MATLAB version of the above snippet. I am sure there is a better way to do it in MATLAB.
Buffer = ' ';
Buffer = repmat(Buffer,1,80);
mystring = 'Velocity';
Buffer(1:numel(mystring)) = mystring;
fid = fopen(filename, 'w');
fwrite(fid, Buffer ,'*char');
Could someone give me a more efficient translation for the above C snippet?
  댓글 수: 1
Walter Roberson
Walter Roberson 2013년 6월 14일
The meaning of the C code you show depends upon whether buffer[80] is defined at file scope or at function scope. If it is at function scope (as seems more likely) then the contents of the buffer after 'C Binary\0' are undefined, whereas if it is at file scope then the contents of the positions after 'C Binary\0' are binary 0.
The correct translation of the code depends upon how many bytes "char" is defined as on the target processor. Typically it is one byte (by definition), but on embedded processing, especially DSPs, a char in C might occupy a 16 bit word or even a 32 bit word.
MATLAB's definition of "char", if you can locate it, requires that char be at least 16 bits, but it is not clear if you want the buffer to be filled with 8 bit characters (most likely with C but not the formal definition in C!) or with 16 bit characters (closer in spirit to C's wchar)

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

답변 (3개)

David Sanchez
David Sanchez 2013년 6월 14일
Are you trying to write a file with "velocity" in it?
filename='test'; % or whatever name you want
Buffer = 'Velocity';
fid = fopen(filename, 'w');
fwrite(fid, Buffer ,'*char');
fclose(fid)
Do not forget to close the file after using it.
  댓글 수: 1
AP
AP 2013년 6월 14일
편집: AP 2013년 6월 14일
I want to write any string less than 80 characters to a file. However, I want the string to occupy 80 characters. I don't want to write a string with for example 10 characters.

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


David Sanchez
David Sanchez 2013년 6월 14일
This is a bit long, but it works.
Buffer = 'Velocity'; % your string
my_str = cell(80,1); % initialize a cell to hold characters
for k = 1:size(my_str)
my_str{k} = ' ';
end
for k2 = 1:size(Buffer,2)
my_str{k2} = Buffer(k2);
end
final_str = '';
for k3=1:size(my_str)
final_str = horzcat(final_str,my_str{k3}); % you get an 80 character string, checl it out with "length(final_str)"
end
filename='test'; % or whatever name you want
fid = fopen(filename, 'w');
fwrite(fid, final_str ,'*char');
fclose(fid);

Walter Roberson
Walter Roberson 2013년 6월 14일
filename='test'; % or whatever name you want
OutString = 'Velocity';
Buffer = uint8(255*rand(1, 80));
Buffer(1:length(OutString)) = OutString;
Buffer(length(OutString)+1) = 0;
fid = fopen(filename, 'w');
fwrite(fid, Buffer);
fclose(fid)
Or
filename='test'; % or whatever name you want
OutString = 'Velocity';
Noise = uint8(255*rand(1, 80 - length(OutString) - 1));
fid = fopen(filename, 'w');
fwrite(fid, OutString, 'uint8');
fwrite(fid, 0, 'uint8');
fwrite(fid, Noise);
fclose(fid);
Unless, that is, you have a need for the entire writing process to be atomic.
If you do not need OutString to be a variable, you can get more compact. And if you don't mind assuming that the buffer will start out zeroed, then:
filename='test'; % or whatever name you want
Buffer = zeros(1,80,'uint8');
OutString = 'Velocity';
Buffer(1:length(OutString)) = uint8(OutString);
fid = fopen(filename, 'w');
fwrite(fid, Buffer);
fclose(fid)
If you need it to start out blanks (which would not be the case in C unless by pure accident), then remember to null the byte after the string if you need strcpy() conformance.
Of course there is
fid = fopen('test', 'w');
fwrite(fid, [uint8('Velocity'), zeros(1, 72, 'uint8'));
fclose(fid)
Note: the original C code assumed that the file was already open and named and as not to be closed, so the MATLAB versions look to be longer because they are taking care of those details as well as writing the data.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by