how view the content cell array

5 visualizzazioni (ultimi 30 giorni)
Luca Re
Luca Re il 22 Dic 2023
Modificato: Dyuman Joshi il 22 Dic 2023
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 Commenti
Stephen23
Stephen23 il 22 Dic 2023
regexp(C{:},'(\w+)','tokens');
% ^ this will produce unexpected results if C is nonscalar.
Dyuman Joshi
Dyuman Joshi il 22 Dic 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.

Accedi per commentare.

Risposta accettata

Stephen23
Stephen23 il 22 Dic 2023
Modificato: Stephen23 il 22 Dic 2023
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'

Più risposte (1)

Hassaan
Hassaan il 22 Dic 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 Commento
Stephen23
Stephen23 il 22 Dic 2023
Modificato: Stephen23 il 22 Dic 2023
"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:

Accedi per commentare.

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by