how view the content cell array
Show older comments
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 Comments
Dyuman Joshi
on 22 Dec 2023
Why do you have the text in a cell array? Why not use character or string array?
shamal
on 22 Dec 2023
Stephen23
on 22 Dec 2023
"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:
Dyuman Joshi
on 22 Dec 2023
Edited: Dyuman Joshi
on 22 Dec 2023
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
on 22 Dec 2023
regexp(C{:},'(\w+)','tokens');
% ^ this will produce unexpected results if C is nonscalar.
Dyuman Joshi
on 22 Dec 2023
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.
Accepted Answer
More Answers (1)
Hassaan
on 22 Dec 2023
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 Comment
"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:
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!