MATLAB Answers

How do I extract all rows of a struct field that is a character array?

122 views (last 30 days)
Pat Canny
Pat Canny on 6 Dec 2019
Commented: Gordon Lai on 5 Mar 2021
I want to extract all rows of a field within a struct. The field is a character array.
For sake of example, the struct is called systemData, with a field called Objects, which has a field called status (among many others). The status field is a char, and has 20 rows.
I'd like to extract all 20 rows of systemData.systemObjects.status
When I try "normal" index operations, I either only get one result, or I get an error.
Here is one attempt, which results in an error:
systemData.Objects.status{:}
Error: Expected one output from a curly brace or dot indexing expression, but there were 20 results.
I also tried this:
test_extract = systemData.Objects.status;
But I only get the first row.
How do I extract all rows?

Accepted Answer

Pat Canny
Pat Canny on 6 Dec 2019
First, try to avoid using character arrays. Convert them to string arrays.
Second, you need to use the {curly brace syntax} to extract multiple elements.
Try this:
object_status = string({systemData.Objects.status})';
  3 Comments
Gordon Lai
Gordon Lai on 5 Mar 2021
Thanks, I have had this problem using the "dir" command...!
If strings are recommended, is there a reason for using char arrays as the default struct field for "dir"?

Sign in to comment.

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by