Output from nested struct into numerical / string array

조회 수: 26 (최근 30일)
mahoromax
mahoromax 2019년 8월 9일
편집: Stephen23 2022년 2월 23일
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