How can I assign values to a changing string in a loop?

5 views (last 30 days)
Hi all,
Ive spent a week trying to do this and I'm literally pulling my hair out. Any help would be massively appreciated!
I have called 16 variable in a loop: variable 'wl11' through to 'wl116' using the code below. The 16 variables (wl11 through to wl116) each have values 501x8(double). Just for explanation purposes, Each value is an EMG reading of 501 samples using 8 EMG sensors around the circumference of the forearm.
I am calling each value in my loop and I want to return a feature for each movement, in this case a function calculating the Maximum Absolute Value(MAV) of each sensor in the movement, so the function getMAV returns an 8x1 array (The MAV for each sensor for the entire movement) using the specific variable called (wl11...wl116).
My diffuculty arises when I successfully create, within the loop, the string (wl11_mav) for the name of the returned function value (8x1), but the string cannot take a value (as it is only a string). ive tried assignin and genvarname and cant get them to work.
I want to name this returned function value in the loop as 'wl11_mav', then continue looping as the 16 MAV arrays are being returned and each titled appropriatly: wl11_mav , wl112_mav etc.
I also want to read in the 16 no. (8x1) arrays (the returned function value) into a 3 dimensional array called dataMAV(8,1,16)
I hope I havent confused everyone to death...Here is my code so far below which includes my loop in my main script and the function im calling:
MAIN SCRIPT:
for j = 1:16 Root='wl1'; findmov=eval([Root,num2str(j)]);
ending='_mav'; %x=zeros(8,1);
x=getMAV(findmov);
y=[Root,num2str(j),ending];
assignin(workspace, y, x)
dataMAV=zeros(8,1,16); dataMAV(:,:,j)=read(x);
end
FUNCTION FOR MAV:
% mean absolute value for one movement all channels - channels 1 to 8 from % myo armband
function MAV=getMAV(mov);
x=mov;
y=abs(x);
for i=1:8;
k(i)=sum(y(:,i));
end
MAV=k';
end
  1 Comment
Stephen23
Stephen23 on 14 Jul 2015
Edited: Stephen23 on 19 Jun 2019
Naming variables dynamically is a really bad practice, and should be avoided:
Make you own life easier by using an array (numeric, cell, structure) or a table instead.
"My difficulty arises" through trying to use a poor programming practice. Program properly using arrays and indexing and this whole problem goes away. And if you think it is hard to create these variable names dynamically, just imagine the buggy spaghetti code that is required to try to use them.
If you put all of these array in a cell array C then merging them is really easy: cell2mat(C) or cat(1,C{:}) or something similar. Doing this with dynamically named variables will be even buggier than the code used to create them.

Sign in to comment.

Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 14 Jul 2015

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!