How do I take spaces out of a string array with multiple rows?

I've searched and found various solutions to this problem that work when you have a variable that has only 1 row. For example:
BCD_space_1row ='1 0 0 1'
%BCD_space_1row has extra spaces that needs to be deleted
BCD_space_1row(isspace(BCD_space_1row)) = []
%this takes out the extra spaces in BCD_space_1row
The result when I run this is:
BCD_space_1row =
1 0 0 1
BCD_space_1row =
1001
which is exactly what I want. This works fine for an array with 1 row but crashes when there are multiple rows. For example:
BCD_space_multirows(1,:) = '1 0 0 1'
BCD_space_multirows(2,:) = '0 1 1 0'
BCD_space_multirows(isspace(BCD_space_multirows)) = []
I want the result after taking out the spaces to be this:
BCD_space_multirows(1,:) = '1001'
BCD_space_multirows(2,:) = '0110'
but my result from above is
BCD_space_multirows =
10010110
I don't understand why the isspace command is giving me this result.
Attempting to run isspace function on 1 row at a time doesn't work either as both examples below also fail
for x = 1,2
BCD_space_multirows(x,:)(isspace(BCD_space_multirows(x,:))) = []
end
BCD_space_multirows(1,:)(isspace(BCD_space_multirows(1,:))) = []
Thanks for any help. Sorry I had to double space everything but when I didn't the preview show no line feeds and I couldn't figure out how to get only a single linefeed. Also the preview seems to have changed from my fixed pitch font (that's better for showing blank spaces) into a proportional font but I think people get the idea - I want all spaces within the string deleted but I need to retain multiple rows and not have all my text concatenated into 1 row.

 采纳的回答

s='1 0 1 0 '
s(strfind(s,' '))=[]
for multiple rows
b(1,:) = '1 0 0 1'
b(2,:) = '0 1 1 0'
idx=strfind(b(1,:),' ')
b(:,idx)=[]

3 个评论

Thanks for the quick reply! I think this does solve my problem, but the way I understand it, it'll only work if my spaces are indexed identically in all rows right? Which is why you're getting the index of spaces from row 1? If for some reason b(1:)='100 0 1' and b(2:)='0 1 1 0' or b(3,:)='1 1 0') (which luckily wouldn't happen in my case) this solution wouldn't work? Just trying to fully understand the solution and its limitations if any. Thanks for a solution to my problem!
Oops - note that in my examples in my comment above, I intended to have all the string sizes be the same (i.e. b(3,:) should have had 3 spaces between the 1's but this editor truncated them and only put 1. Guess I need to read that tutorial on how to format questions that Walter gave me a link to!
But frankly, I don't get why a programming forum where the correct number of spaces and proper line feeds are critical would have an editor that allows you to type in a fixed pitch font and then displays in a proportional font that truncates spaces at will and chooses to ignore your linefeeds! More of Matlab's mystery's to me I guess.
clear b
b(1,:) = '1 0 0 1'
b(2,:) = '0 1 1 0'
out=arrayfun(@(x) b(x,regexp(b(x,:),'[0-1]')),(1:size(b,1))','un',0)
char(out)

请先登录,再进行评论。

更多回答(1 个)

If you are deleting a varying number of blanks from each row of a character array, then the only way to keep it as a string array is to blank pad at the end of lines so that each line remains the same length.
You may wish to consider switching to cell arrays instead, as those can have strings of different lengths.
If you are deleting the same number of blanks from each row of a character array, then after you get the result that is "jammed together", reshape() it to have the number of rows you expect
nrows = size(BCD_space_multirow,1);
BCD_space_multirows(isspace(BCD_space_multirows)) = [];
BCD_space_multirow = reshape(BCD_space_multirow, nrows, []);

2 个评论

Yeah, I see it does get a lot more complicated if the number of spaces isn't the same in each row, then the resulting no_space rows would be unequal length. But what if the number of spaces were the same and just in different locations (indexes)? Then the resulting no_space rows would still be equal length, but using the solution Azzi presented wouldn't work because the indices where the spaces are in each row wouldn't be the same anymore.
Anyway, that's not what I have so that's no worry for me. Just thinking how to handle a case like that. I do see how you'd probably have to reshape it to have the number of rows you expect as Walter suggested. Thanks for all the help. This conversion from numbers to BCD (binary coded decimal) and back again is a real PITA in the way Matlab's functions don't seem to "play well" together (converting from numbers to strings (dec2bin), and then strings with added spaces at that, and then conversion back to numbers (str2double) to do a one's complement (bitcmp, but leading 0 are truncated so they must be added back) and then conversion back to string (num2str) but only if there are no added spaces so you have to take them out, etc. It's a nightmare.
Oh... just realized that the above reshape() will not work if the blanks are not nicely lined up.
Anyhow, what step is it that you are needing to add spaces for, or that is resulting in unwanted spaces?
BCD = ['0000'; '0001'; '0010'; '0011'; '0100'; '0101'; '0111'; '1000'; '1001'];
BCD_encoded = BCD(sprintf('%d', TheNumber) - '0' + 1, :);

请先登录,再进行评论。

类别

在 帮助中心 和 File 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