How can I use the split function with multiple delimiters?

I am trying to use the split command on a cell Array but I am getting this error:
Element 77 of the text contains 2 delimiters while the previous elements have 1. All
elements must contain the same number of delimiters.
The problem is that in the line below
software_logical/forIteratorSubsystem/Out1. Some of the elements in my have different number of delimiters.
software_math/sum
This the code I am using below. What can I do so that my program will split at the last delimiter ('/')
cellArray = split(compareBlocks,'/');
cellArray = split(compareBlocks,'/',2); %I even tride using this but it does not work

 채택된 답변

Stephen23
Stephen23 2020년 2월 27일
편집: Stephen23 2020년 2월 27일
>> str = 'software_logical/forIteratorSubsystem/Out1';
>> [one,two] = fileparts(str)
one = software_logical/forIteratorSubsystem
two = Out1
>> str = 'software_math/sum';
>> [one,two] = fileparts(str)
one = software_math
two = sum
For character vectors in a cell array call fileparts inside of cellfun.

댓글 수: 4

I see what you did, and I should have explained more what I am doing. I am getting block from the simulink model, and I am getting some from a diagram to compare them and make sure they came from the correct library. I tried your method in a way to fit what I am doing but I am getting this error message.
Input must be a row vector of characters.
I tried turning the column vector into a row vector and got the same response.
@Deon Hargrove: did you try the advice I gave at the end of my answer?
Sure did. Played around with the various ways I could use it.
This is the most recent
cellArray = cell(fileparts(compareBlocks))
cellArray = cell(fileparts(compareBlocks))
will place the first output (i.e. the "path") of the split compareBLocks variable into a scalar cell array, and discards the rest. There does not seem to be much point in doing that.
Lets try my answer on a cell array, using cellfun as I mentioned:
>> C = {'software_logical/forIteratorSubsystem/Out1', 'software_math/sum'};
>> [one,two] = cellfun(@fileparts, C, 'uni',0);
>> one{:}
ans = software_logical/forIteratorSubsystem
ans = software_math
>> two{:}
ans = Out1
ans = sum

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Akira Agata
Akira Agata 2020년 2월 28일
How about using cellfun, like:
% Sample cell array
C = {'software_logical/forIteratorSubsystem/Out1', 'software_math/sum'};
% Apply 'split' for each cell
parts = cellfun(@(x) split(x,'/'),C,'UniformOutput',false);
The result is like this:
>> parts{1}
ans =
3×1 cell array
{'software_logical' }
{'forIteratorSubsystem'}
{'Out1' }
>> parts{2}
ans =
2×1 cell array
{'software_math'}
{'sum' }

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

질문:

2020년 2월 27일

댓글:

2020년 2월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by