Getting index from strsplit or strfind in a null (00) terminated string

I have a long string with chunks of data. Each data chunk is separated/terminated by null (hex 00). Right now I use strsplit(data, '\0') to split the string, which works fine, but it returns a cell array with data, however, I rather need the functionality of strfind and get a list of indices for where the string is split.
Example of data:
data_chunk "null/0x00" data_chunk "null/0x00" data_chunk "null/0x00"
I know the first chunk is obviously at index 0, but I would also like to get the indices of the remaining chunks.
Is it possible to use strfind and search for null?

2 Kommentare

Post clearly your data, for example:
str='null/0x00'
Then explain what is the expected result
result=?
For reference, an example of the string could be
str = 'engine_thrust combustion yaw pitch roll'
where the whitespaces are char null i.e. hex value 0x00.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

per isakson
per isakson am 10 Apr. 2016
Bearbeitet: per isakson am 10 Apr. 2016
What am I missing?
>> str = [char(0),'abc',char(0),'123',char(0)];
>> find( str==char(0) )
ans =
1 5 9
>> strfind( str, char(0) )
ans =
1 5 9
>> strsplit( str, '\0')
ans =
'' 'abc' '123' ''

4 Kommentare

You are not missing anything, that is the answer I was looking for. I was not aware that char(0) would yield that result and could be used for the search pattern.
Thank you.
Simon, don't concentrate on that. The char() casting is not needed. This works just fine as you'd expect:
str = [0, 'abc', 0, '123', 0];
nullLocations = find(str == 0)
subStrigns = strsplit(str, '\0')
Great, nice to know, might as well keep it as clean as possible.
IMO: Explicit casting makes the code easier to read (and often faster).
>> ['B'+1, 67]
ans =
67 67
>> ['B', 67]
ans =
BC
>> ['B'+1, 'C']
ans =
CC

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 10 Apr. 2016

Bearbeitet:

am 11 Apr. 2016

Community Treasure Hunt

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

Start Hunting!

Translated by