Output from nested struct into numerical / string array
12 views (last 30 days)
Show older comments
Hello together,
I'm a bit bothered that I dont find a way to do this in a elegant way. Lets say I receive some output from a struct like this:
mystruct.somenesting.objecttype.name
mystruct.somenesting.somethingelse.size
both will return me a list of names (char arrays) or sizes (double), but not as an array, but as 'selected' outputs (ans = ' ' , ans = '')
I can only "catch" this output into a cell array like this:
CA = {mystruct.somenesting.objecttype.name}
I would like to directly put it into a string array, or a double array without first saving it as cell array and then either transforming it (cell2string) or using a loop to go through each element and convert the elements.
It feels like I'm just missing a little trick with brackets :-/
A similar issue arises if I just want to read the contents of the cell array into a new array by trying to evaluate all cells with :
valueCA = {mystrut.some.thing.value}
valuearray = [valueCA{:}];
valuearray(:) = valueCA{:};
They're both wrong. I need the {:}, that I'm sure off, but I dont know how to read each value into another variable so it doesnt overwrite each other.
Edit:
Appended an example struct
stringarray = string({msmtrun.pipelines.name});
does work, I just hoped there was a smoother way to do it (without a function call)
cellarray = {msmtrun.pipelines.name};
stringarray = cellarray(:); %Not a string array
I imagine my problem lies there, that the cells are filled with char arrays, which need to be converted into a string first to build a string array.
Similarly I cant get the contents of all cells into one (1x) char array, bc that would mean loosing the seperations from the cell. And I cant get it into a multidimensional char array, bc the char arrays have different lengths.
As for the double, I think it was a bracket error, I cant reproduce it for now. If I access
msmtrun.pipelines(1,1).data(1,1).data
I get an array of doubles as expected.
5 Comments
Stephen23
on 9 Aug 2019
Edited: Stephen23
on 9 Aug 2019
string(mystruct.somenesting.objecttype.name)
to not throw an error? That comma-separated list likely has far too many inputs for string, nor does string have a syntax with an aribitrary number of input arguments: string converts its first input argument to a string array, whereas you are passing it multiple inputs (as the links in my answer explain).
'As for the expected output array, I think I specified it as "string array" and "double array".'
Have you tried the code in my answer?
Accepted Answer
Stephen23
on 9 Aug 2019
Edited: Stephen23
on 23 Feb 2022
string({mystruct.somenesting.objecttype.name})
cat(1,mystruct.somenesting.somethingelse.size)
% ^ change this to suit the sizes of your data
Read these:
4 Comments
Stephen23
on 9 Aug 2019
Edited: Stephen23
on 9 Aug 2019
"I just didnt want to immediately, in case I would find closely related questions."
You are welcome to take your time, and I fully support looking for other/better solutions. It is unfortunately common that some people just disappear as soon as they get a solution :(
I also recommend reading the string documentation carefully, and the links in my answer.
More Answers (0)
See Also
Categories
Find more on Cell Arrays 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!