Output from nested struct into numerical / string array

14 visualizaciones (últimos 30 días)
mahoromax
mahoromax el 9 de Ag. de 2019
Editada: Stephen23 el 23 de Feb. de 2022
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 comentarios
Stephen23
Stephen23 el 9 de Ag. de 2019
Editada: Stephen23 el 9 de Ag. de 2019
@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 el 9 de Ag. de 2019
Editada: mahoromax el 9 de Ag. de 2019
@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.

Iniciar sesión para comentar.

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Cell Arrays en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by