How to make a structure from structures?

17 visualizaciones (últimos 30 días)
Damian
Damian el 29 de En. de 2018
Comentada: Stephen23 el 31 de En. de 2018
I have variables in workspace like this: from P1 to P28 (structure array). I want to save them in P to call them like this (these are points for my robot):
MOVE1 = rmove( Kawasaki, P(1:28) );
  2 comentarios
Stephen23
Stephen23 el 29 de En. de 2018
Editada: Stephen23 el 29 de En. de 2018
"I have variables in workspace like this: from P1 to P28 (structure array)"
Inefficient data design is the start of many problems.
How did you get those structures into MATLAB memory?
The best solution is to avoid having lots of numbered variables and trying to access variable names magically. Magically accessing variable names is how beginners force themselves into writing slow, complex, buggy code which is hard to debug. Read this to know more:
Whether importing data or generating it in a loop there is almost always a much simpler way to store that data in one array using indexing.

Iniciar sesión para comentar.

Respuesta aceptada

Damian
Damian el 31 de En. de 2018
Editada: Damian el 31 de En. de 2018
How i repair it:
Make P:
P = [ rpoint(0,0,0,0,0,0,'signal', [],'pulse', [], 'wait', 0) ];
and then I make
P(1) = rpoint(265.828,648.913,-165.654,0,0,0,'signal', [],'pulse', [], 'wait', 1);
P(2)
.
.
.
.etc
Thank you for trying to help me.
  1 comentario
Stephen23
Stephen23 el 31 de En. de 2018
That is a good solution. You could also use a loop.

Iniciar sesión para comentar.

Más respuestas (3)

Walter Roberson
Walter Roberson el 29 de En. de 2018

Damian
Damian el 29 de En. de 2018
Editada: Damian el 29 de En. de 2018
I have P1,P2,P3 already in workspace. I want to group them to P. IT block diagram look like this. Please dont send me any tips "how to" blablabla. I need only one good line to group this stuff. It shoud have this look because of robots's toolbox.
Here are good example but i don't know exactly how to use it in my case. EXAMPLE
  2 comentarios
Walter Roberson
Walter Roberson el 29 de En. de 2018
There are no good lines to do what you are asking. You should not create those variables in the first place -- you should have stored them in a cell array or struct or multidimensional array.
Stephen23
Stephen23 el 29 de En. de 2018
Editada: Stephen23 el 29 de En. de 2018
"Please dont send me any tips "how to" blablabla. I need only one good line to group this stuff"
Read the link that Walter Roberson gave you: the answer to your question is mentioned in that thread seventy-three times. Did you miss them?
It also contains explanations of why that approach makes beginners' code slow, complex, and buggy. Good luck finding "one good line": I promise you it will make your code slow, complex, and make debugging more difficult.

Iniciar sesión para comentar.


Jos (10584)
Jos (10584) el 30 de En. de 2018
If A, B, etc. are similar structures you can simply concatenate them. An example
% data
A = struct('x', 1, 'y', 2)
B = struct('x', 7, 'y', 8)
% engine
S = [A B]
But you would be better off in most cases when you did not need to create A and B in the first place ...

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by