Split information into two columns
조회 수: 12 (최근 30일)
이전 댓글 표시
Hello, I have an excel file and it has 2 columns and a lot of lines. The first column has numbers and the second column has words like this "House_red" or "House_x_red" or "House". I used this to extract the second column
[~,houses] = xlsread('houses.xlsx', 'B:B');
And right now I have all my information separated numbers from strings, but in the strings, I want to have a column with House and the other with Red or x_red, where house will be in the first column and red or x_red in the second column. Is there a way to split a string into two columns knowing that one line can be "House_Red" and another line is "House_X_Red"?
댓글 수: 2
Jan
2018년 6월 28일
I don't get what you want. Can you post a small example of the inputs and the wanted output?
채택된 답변
Star Strider
2018년 6월 28일
Try this:
strs = {"House_red"; "House_x_red"; "House"; "Apartment"}; % Data
chrs = cellfun(@char, strs, 'Uni',0); % Convert ‘string’ To ‘char’
L1 = cellfun(@isempty, strfind(chrs, '_')); % Find Elements Without Underscores ‘_’
chrs(L1) = cellfun(@(x)sprintf('%s_', x), chrs(L1), 'Uni',0); % Add Terminating Underscores To Them
sep = regexp(chrs, '_', 'split', 'once'); % Split On First Underscore
H = cellfun(@(x)x(:,1), sep);
C = cellfun(@(x)x(:,2), sep);
HC = [H, C]
HC =
4×2 cell array
{'House' } {'red' }
{'House' } {'x_red' }
{'House' } {0×0 char}
{'Apartment'} {0×0 char}
You indicated that your original data was a string array, so I included a step that converts it to a char array. The code resolves the problem of "House" not having an underscore (_) by adding one before splitting the char array ‘chrs’ with by the first underscore. This is the easiest way I can devise to create an appropriate output for ‘sep’, so that each row vector is a (1x2) cell array. After that, the rest is straightforward.
추가 답변 (1개)
Jan
2018년 6월 28일
편집: Jan
2018년 6월 28일
Str = {'House_Red'; ...
'House_Yellow'; ...
'House'; ...
'House_X_Red'; ...
'House_X_Purple'};
[C1, C2] = strtok(Str, '_'); % Split strings at 1st _
C2 = cellfun(@(x) x(2:end), C2, 'UniformOutput', 0); % Remove leading _
C2(cellfun('isempty', C2)) = {'undefined'}; % If wanted
Result = [C1, C2] % Join columns
댓글 수: 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!