How to save and change the position of some fields in a structure?

3 visualizaciones (últimos 30 días)
I have a matrix of buttons (the user choses the number os lines and columns) presented in figure 1. When the user choses the number of lines and columns, there is a struct called sfiles_info which is created with all the combinations of pairs of buttons like in figure 2. When I press the S parameters button, I chose some files of parameters and acording the names of the files, they go to the correct place in the struct - figures 3 and 4. In this case I have 1,1 with 1,2 and 2,2 with 1,2. So in the structure for the first file I will have S parameters in A1 = 1,1 , A2 = 1,2 and A1 = 1,2 , A2 = 1,1. And the same for the other file. So my problem is if after this, the user changes the length of the array, for example 3 lines and 2 columns (figure 5), I have all the combinations in the structure as in the beginnig. But as the combinations are different, the struct changes the A1 and A2 positions - figure 6. What I can't do is as the A1 and A2 changes, it was nice if the other fields changes too for the correct place. But I can't. For exmaple, in figure 6, the parameters should be in A1 = 1,1 and A2 = 2,1 and not A1 = 1,1 and A2 = 3,1.
I am sorry for the long description, I hope you understood my question. In the next figures I have the code I write:
  • When the user choses the number of lines and columns - figure 7
  • When the user presses S Parameters button - figure 8
  • After having the S parameters for the files (two files in this case), next function to write them in the oposite A1 and A2, to stay with 4 lines of S parameters - figure 9
So when the user changes the number of lines/cols, figure 7 is done again. Is there a way to change the place of my parameters according to the place where they are before?
I hope you could help me with an ideia or something. Thank you! :)

Respuesta aceptada

Stephen23
Stephen23 el 16 de Mzo. de 2022
Editada: Stephen23 el 16 de Mzo. de 2022
I think what you are looking for can be achieved using ISMEMBER.
Basically you need:
  • a way to identify any matches between the old and new values of A1 and A2
  • a way to give their relative locations.
That is what ISMEMBER can do.
An rough outline of the steps:
  1. create two string arrays, each with two columns for A1 and A2, for both the old and new data.
  2. call ISMEMBER with both of those arrays and the 'rows' option. I think you will need old as the 1st input and new as the 2nd input, but of course you should check this yourself.
  3. Obtain both output arguments.
  4. Use the 1st output to select which existing rows to keep, which to discard.
  5. Use the 2nd output to allocate the oldAn elements into the newAn array.
Pseudocode:
oldM = [oldA1(:),oldA2(:)]; % Nx2 string array
newM = [newA1(:),newA2(:)]; % Nx2 string array
[X,Y] = ismember(oldM,newM,'rows');
newAn(Y(X)) = oldAn(X); % repeat for n = 1 & 2

Más respuestas (0)

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by