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
2 次查看(过去 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?
0 个评论
采纳的回答
更多回答(1 个)
Walter Roberson
2021-5-10
strjoin() defaults to putting space between the parts. Use a second parameter of '' to not put in spaces.
0 个评论
另请参阅
类别
在 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!