Output from nested struct into numerical / string array

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 个评论

What about
string(mystruct.somenesting.objecttype.name) %?
"...by trying to evaluate all cells ..."
Cell arrays contain data: data cannot be evaluated, only code can be evaluated. Therefore it is unclear what you actually mean. Please explain what you expect to happen.
Most useful would be a complete MWE, including all input arrays and expected output arrays. Otherwise we will just spend a lot of time guessing the classes/sized of that data (which so far you have not explained at all).
Just using
string(mystruct.somenesting.objecttype.name) %?
returns
Error using string
No constructor 'string' with matching signature found.
I apologize for my imperfect terminology. But when talking about cell's we should now that you can access the cell itself and its contents - using () vs {} brackets for example. I dont think its a faar stretch to guess that with the value I mean the contents of the cell.
I will append a matlab file of a dummy structure to the question.
As for the expected output array, I think I specified it as "string array" and "double array".
@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?
@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.

请先登录,再进行评论。

 采纳的回答

string({mystruct.somenesting.objecttype.name})
cat(1,mystruct.somenesting.somethingelse.size)
% ^ change this to suit the sizes of your data
Read these:

4 个评论

I think these are the only ways, since I misunderstood that I wanted to not only rebuilt but change the class of my entries.
Your code works, I just thought before there was a way without feeding it into a function (like string()).
Thx for the useful links, the documentation is soo big, but from the entry 'array' I did not find my way to those. I'm still going through them.
"Your code works"
I am glad! You can show your appreciation by accepting my answer.
I will, I just didnt want to immediately, in case I would find closely related questions.
The impatience XD
"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 个)

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

产品

版本

R2018b

标签

Community Treasure Hunt

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

Start Hunting!

Translated by