unknown character element (after import from binary)
2 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
その他の回答 (1 件)
Image Analyst
2013 年 3 月 21 日
How about deleting anything less than ASCII 32 or more than 'z':
DatabaseName(DatabaseName < 32) = [];
DatabaseName(DatabaseName > 'z') = [];
2 件のコメント
Image Analyst
2013 年 3 月 21 日
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.
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!