MATLAB: Randomization of columns of 3 cell arrays and accessing data

evalrandomizationrandperm

The main goal is to randomize the columns of 3 cell arrays. The names of the cell arrays are:
names = {'PairsEMG_walk','PairsEMG_up','PairsEMG_do'};
I have created an random index array Idx of length(#columnsPairsEMG_walk+#columnsPairsEMG_up+#columnsPairsEMG_do).
Its contents are values that relates to: integer = name of array and decimal = number of column of cell array I am tying to create a new cell array concatenating the randomize data using
condition = floor(Idx); %to point to the cell array column to extract the name of my cell array
column =100*rem(Idx,1); % to pint to the column of "that" cell array
For example, I have:
Idx = [ 2.0400 3.1200 1.1600 1.0900 2.0800 2.0200 3.1400 3.0200 3.0600 3.0100 1.1200 1.0200 2.1400 3.1500 1.1000 3.1300 2.1000 1.0400 2.1500 3.0900 2.0900 3.1100 2.1200 2.0100 1.0800 3.1000 1.0100 1.1400 3.0500 3.0400 1.0300 3.0800 1.0500 1.0600 1.0700 2.0500 2.1100 2.0600 1.1300 3.0700 2.0700 3.0300 2.1300 1.1100 2.0300 1.1500];
Therefore, the first value of condition is 2 and column 4, so I have to extract column 4 from PairsEMG_up and put it in EMGInputSeries, for the next iteration, I would need to extract column 12 from PairsEMG_do, and so on.
I tried to use eval(), but I am getting an error
for i=1:length(Idx)
EMGInputSeries{i}=eval([names(condition(i)),'{',column(i),'}'])
end
I get: Error using eval Argument must contain a character vector.
also tried:
eval(['names(',condition(i),'){',column(i),'}'])
and I get Error: The input character is not valid in MATLAB statements or expressions.
FYI, the PairEMG arrays are 3×15 cell array, and need to extract all rows of "that" column.
Thank you,

Best Answer

eval is, like always, a red-herring that just distracts and misleads from finding real solutions. Beginners do this because they think that eval is a magic tool that will solve all of their problems, but then they get stuck trying to solve all of the pointless problems that eval causes. And so they persist in fighting eval instead of looking for simpler, neater, much more efficient, much less buggy solutions. Sigh.
"The main goal is to randomize the columns of 3 cell arrays"
Well that is easy, so lets do that:
C = {PairsEMG_walk,PairsEMG_up,PairsEMG_do};
for k = 1:numel(C)
C{k} = C{k}(:,randperm(size(C{k},2)));
end
To use your specific random column order index that you would like to use, we just replace the randperm operation with that index. Note that hiding indices in decimal fractions is.... innovative, but awkwardly complex:
idx = [2.0400 3.1200 1.1600 1.0900 2.0800 2.0200...]
ida = fix(idx);
idc = round(100*rem(idx,1)); % rounding avoids floating point problems
Personally I would keep all indices as whole numbers, which is easy by using two variables to start with (much simpler, easier to work with, no limit to the number of columns, more robust, and avoids any floating point issues entirely):
ida = [2, 3, 1, 1, 2, 2, ... ] % array index
idc = [4, 12, 16, 9, 8, 20, ... ] % column index
In any case, you can then use those two index vectors quite easily:
C = {PairsEMG_walk,PairsEMG_up,PairsEMG_do};
N = numel(ida);
D = cell(1,N);
for k = 1:N
D{k} = C{ida(k)}(:,idc(k));
end
There you are, the columns of the arrays within C are given in D in the random order that you have requested, and without using an evil eval anywhere. If that output is not what you want, then specify more precisely what you are actually trying to do and we will help you with that.