WTF... sorry folks, I might be a bit tired from coding. I took things from c and didn't pay attention to the fact that break; in matlab obviously leaves the switch case.
assign characters in a for loop combined with switch case and then merge to a long string
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Christian
el 10 de Mayo de 2021
Respondida: Christian
el 10 de Mayo de 2021
Hi all!
I want to assign a string array with one column and many rows containing binary values to an alphabetical character. However, this alphabetical character array should be transposed. Thus, it has only one row and many columns. In the last step, i want to combine all these columns into one cell using strjoin().
bitstream = 120×1 string array
"00001"
"00010"
"00011"
"00000"
"00001"
"Error"
"00011"
...
if exist('bitstream','var') ~= 0 % if bitstream exist, get into
for idx = 1:size(bitstream,1)
switch bitstream(idx,1)
case "00000"
outputText(1, idx) = " ";
break;
case "00001"
outputText(1, idx) = "A";
break;
case "00010"
outputText(1, idx) = "B";
break;
case "00011"
outputText(1, idx) = "C";
break;
otherwise
outputText(1, idx) = "_";
end
end
outputDisplay(1,1:end) = strjoin(outputText)
end
The result should look like outputDisplay = "ABC A_C"
There is no error in my code, but it doesnt work and I couldn't figure out why... In most cases he jumps way to early out of the for loop. But it is not consistent...
outputText =
Columns 1 through 21
"A" "B" % 118 values should follow here!
Is a query of strings within a switch case query like case "00001" not possible? Should I use if, else if statements instead?
0 comentarios
Respuesta aceptada
Más respuestas (1)
Walter Roberson
el 10 de Mayo de 2021
strjoin() defaults to putting space between the parts. Use a second parameter of '' to not put in spaces.
0 comentarios
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!