Output from nested struct into numerical / string array

14 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2019-8-9
编辑:Stephen23 2019-8-9
@mahoromax: why do you expect
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?
mahoromax
mahoromax 2019-8-9
编辑:mahoromax 2019-8-9
@Stephen Cobeldick
I was just about to comment your answer, I just wanted to answer these comments first.
As for the error message, it was in return to the first comment I got.

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2019-8-9
编辑:Stephen23 2022-2-23
string({mystruct.somenesting.objecttype.name})
cat(1,mystruct.somenesting.somethingelse.size)
% ^ change this to suit the sizes of your data
Read these:
  4 个评论
mahoromax
mahoromax 2019-8-9
I will, I just didnt want to immediately, in case I would find closely related questions.
The impatience XD
Stephen23
Stephen23 2019-8-9
编辑:Stephen23 2019-8-9
"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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

标签

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by