how view the content cell array

조회 수: 5 (최근 30일)
Luca Re
Luca Re 2023년 12월 22일
편집: Dyuman Joshi 2023년 12월 22일
C={' fdfd(fg,54)'}
C = 1×1 cell array
{' fdfd(fg,54)'}
W1 = regexp(C,'(\w+)','tokens')'
W1 = 1×1 cell array
{1×3 cell}
{W1}
ans = 1×1 cell array
{1×1 cell}
How can I view the content? i try {W1}
but it does not work
  댓글 수: 6
Stephen23
Stephen23 2023년 12월 22일
regexp(C{:},'(\w+)','tokens');
% ^ this will produce unexpected results if C is nonscalar.
Dyuman Joshi
Dyuman Joshi 2023년 12월 22일
Yes, it will give an error, if C is non-scalar
However, as the outcome OP got was a scalar cell array, I used that notation. I will clarify more in my comment.

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

채택된 답변

Stephen23
Stephen23 2023년 12월 22일
편집: Stephen23 2023년 12월 22일
C = {' fdfd(fg,54)'};
Some steps to remove nesting of cell arrays at the output:
  • The cell array is superfluous so index into it.
  • Using TOKENS serves no purpose because you return the entire match... so use MATCH instead.
  • Use the ONCE option.
D = regexp(C{1},'\w+','match','once')
D = 'fdfd'
D = regexp(C{1},'\w+','match')
D = 1×3 cell array
{'fdfd'} {'fg'} {'54'}
D{1}
ans = 'fdfd'

추가 답변 (1개)

Hassaan
Hassaan 2023년 12월 22일
When you use regexp with the 'tokens' option, MATLAB returns a cell array of cell arrays. To access the contents of such a nested cell array, you need to index into the cell array twice.
Here's how you can access the contents of a nested cell array in MATLAB:
Assuming W1 is the result of the regexp operation and is a 1x1 cell array containing another 1x1 cell array, you would access the content like this:
% 'W1' is a 1x1 cell array containing another cell array
content = W1{1}{1};
In this line of code, W1{1} accesses the first cell of W1, which is itself a cell array. Adding another {1} accesses the first cell of this nested cell array.
If W1 were to contain multiple tokens, and you wanted to view all of them, you could use a loop or cell array manipulation functions. For example, if you wanted to create a flat cell array from the nested cell array, you could use:
% Flatten the cell array if there are multiple tokens
flatContent = vertcat(W1{:});
And if you wanted to display the contents of all tokens, you could loop through them:
% Display the contents of each token if multiple tokens are present
for i = 1:length(W1)
token = W1{i};
disp(token{1});
end
This code will print the contents of each token to the MATLAB command window. If there's only one match and one token, the first method with W1{1}{1} is sufficient. If you're working within an app and need to display the content in a UI component like a text area, you would set the text property of that component to content.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
  댓글 수: 1
Stephen23
Stephen23 2023년 12월 22일
편집: Stephen23 2023년 12월 22일
"When you use regexp with the 'tokens' option, MATLAB returns a cell array of cell arrays."
Actually what REGEXP returns depends on what input was provided (char vector vs string scalar vs string array vs cell array of char vectors) and also depends on the ONCE option.
This is explained under 'tokens' here:

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

카테고리

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