how to split a string (char value) with zero in front of it but showing the value in the matrix?

1 Ansicht (letzte 30 Tage)
I have a string of '0100'. i want to split it into binary bit 0 1 0 0. how can i do that?? whenever i turn it into double or integer it turns out 1 0 0,

Akzeptierte Antwort

Stephen23
Stephen23 am 12 Okt. 2022
S = '0100';
V = sscanf(S,'%1u')
V = 4×1
0 1 0 0

Weitere Antworten (2)

John D'Errico
John D'Errico am 12 Okt. 2022
Bearbeitet: John D'Errico am 12 Okt. 2022
MATLAB does NOT allow you to store or represent an integer as the number 0100. If you try, you will get 100. The leading zero goes away. I'm sorry, but it does, and nothing will make that change.
Your simple solution is to NOT convert it into a number. Work with it in terms of strings. Or if you prefer, as a vector of booleans, so 0 or 1 elements.
For example, could you search for leading zeros, counting how many? Yes.

James Tursa
James Tursa am 12 Okt. 2022
Bearbeitet: James Tursa am 12 Okt. 2022
Two more ways:
S = '0100';
S=='0' % logical result
ans = 1×4 logical array
1 0 1 1
S-'0' % double result
ans = 1×4
0 1 0 0

Kategorien

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

Community Treasure Hunt

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

Start Hunting!

Translated by