how view the content cell array
显示 更早的评论
C={' fdfd(fg,54)'}
W1 = regexp(C,'(\w+)','tokens')'
{W1}
How can I view the content? i try {W1}
but it does not work
6 个评论
Dyuman Joshi
2023-12-22
Why do you have the text in a cell array? Why not use character or string array?
shamal
2023-12-22
Stephen23
2023-12-22
"How can I view the content? i try {W1} but it does not work"
Rather than nesting the cell array inside another cell array (what you did), you should try indexing into the cell array using curly braces:
C = {' fdfd(fg,54)'}
% Input is SCALAR CELL ARRAY
W1 = regexp(C,'(\w+)','tokens');
celldisp(W1)
%Input is char array (content of the SCALAR CELL ARRAY)
W1 = regexp(C{:},'(\w+)','tokens');
celldisp(W1)
Stephen23
2023-12-22
regexp(C{:},'(\w+)','tokens');
% ^ this will produce unexpected results if C is nonscalar.
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.
采纳的回答
更多回答(1 个)
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 个评论
"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:
类别
在 帮助中心 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
