MATLAB Answers

unknown character element (after import from binary)

12 views (last 30 days)
usr0815
usr0815 on 21 Mar 2013
Hello,
I have been importing character strings from a binary file, which are results from a command such as:
file = 'I:\20130212_Umean_grid_RE\ExportedData\MatlabTestOnePositionAllProbes.bin';
fileID = fopen(file,'r','n');
fseek(fileID, 10, 'bof');
DatabaseName = fread(fileID,128,'char=>char')';
As result I receive DatabaseName as...
DatabaseName =
F:\20130212_UMEAN_GRID_RE\20130212_UMEAN_GRID_RE.SDB
Now the problem is: DatabaseName is a 1x128 char whereas the first 51 elements (of course this is varying and I don't know that before) are those I am actually interested in and the last 77 elements are absolutly unknown characters, which react on the following commands with
isspace(DatabaseName(end))
ans =
0
ischar(DatabaseName(end))
ans =
1
if DatabaseName(end) == ''
DatabaseName(end) = []
end
DatabaseName --> still (1x128 char)
So the problem I actually have is that I don't find any possibility to compare for those "imaginary/unknown spaces/elements"... so I could delete those with a simple for-loop. Commands such as
strtrim
deblank
don't work obviously because these are based on
isspace == 1
which is not the case for these symbols...
I already opened the binary file with a hex editor and found out that those empty chars are created from 8 bit / 1 byte sequences from the binary file with the binary code "00000000", which corresponds to nul/null in the extended ascii code...
But how is it possible to delete those last elements from the string? How can I look for "nul" in a character string in MATLAB?
Thanks in Advance,
Philipp

  0 Comments

Sign in to comment.

Accepted Answer

Jan
Jan on 21 Mar 2013
Edited: Jan on 25 Mar 2013
I'm surprised that isspace(char(0)) is not TRUE. What about:
Str(Str == char(0)) = [];
Or
Str = Str(Str >= char(32) & Str <= char(127)); % [EDITED && -> &]

  1 Comment

usr0815
usr0815 on 21 Mar 2013
Str = Str(Str >= char(32) && Str <= char(127));
ended up with an error message saying
Operands to the and && operators must be convertible to logical scalar values.
I did not really understand this message, but somehow remembered that sometimes "&" should be used instead of "&&", which I tried and it worked perfectly... Did exactly what I needed!
Thanks a lot... I did not know that one can so simply access all char types by typing char(number 0...127). So it was actually quite simple...
So the best and working solution is:
Str = Str(Str >= char(32) & Str <= char(127));
Thanks a lot!

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 21 Mar 2013
How about deleting anything less than ASCII 32 or more than 'z':
DatabaseName(DatabaseName < 32) = [];
DatabaseName(DatabaseName > 'z') = [];

  2 Comments

usr0815
usr0815 on 21 Mar 2013
Thanks,
this is quite the information I needed... I didn't know that I can access any character type with "char(number 0...127)"... in this way its easy to delete the char(0) elements... although deleting everything that is not ASCII from 32 to 127 is almost the best thing to do.
Image Analyst
Image Analyst on 21 Mar 2013
Yes. I specifically removed non printable ASCII, rather than just 0, because it's a more robust solution, so why not? Your solution, in your comment to Jan, is essentially the converse to mine and equivalent. You're keeping ASCII characters while I removed non-ASCII characters - in the end, they're both equivalent.

Sign in to comment.

Sign in to answer this question.


Translated by