assign characters in a for loop combined with switch case and then merge to a long string

조회 수: 3 (최근 30일)
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?

채택된 답변

Christian
Christian 2021년 5월 10일
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.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2021년 5월 10일
strjoin() defaults to putting space between the parts. Use a second parameter of '' to not put in spaces.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by