0
% Initial Inputs
 S.P1=[0.1,0.2,0.3]; % Varies in length
 S.P2=[3.5,5.5];
 S.P3=[7,8,9,10];

C is 50x3 matrix of integer indices to assign values of P1, P2, P3.

for i=1:numel(fieldnames(S))
      x{i}=['S.P',num2str(i)];
end
x={'S.P1','S.P2','S.P3'};  %Output of for loop

How to use cell name as variable names to extract the values of P1, P2, P3.

[S.P1(C(:,1)),S.P2(C(:,2)),S.P3(C(:,3))] % Desired Output

How to use variable names generated in for loop to extract data as shown above.

user9003011
  • 306
  • 1
  • 10
  • 1
    replace the assignment inside the loop so that it will generate only the field name, without the `S.`, then you can call it dynamically. For the full solution, use inside the loop this line instead what you have: `data = [data ,S.(['P',num2str(i)])(C(:,i))];`, while you define `data = [];` before the loop – Adiel Dec 22 '19 at 13:02
  • Thanks Adiel, Put ans in Answer section. – user9003011 Dec 22 '19 at 13:12

1 Answers1

0

Shortcut way

data=[data,S.(['P',num2str(i)])(C(:,i))]

user9003011
  • 306
  • 1
  • 10