changing matrix names in loops

6 views (last 30 days)
Leelai
Leelai on 14 Dec 2014
Answered: Image Analyst on 14 Dec 2014
I am writing a script to load my data files which are in .mat form. right now the script loops through and loads all the files but the variable names for each file are the same, so every time the script loops the new file over writes the data from the previous file. I tried writing another loop to change the names but that is not working.
for j = 1:6
for i = 1:2
for k = [3,4,6]
if i == 1
i = 'Ground';
else
i = 'Sand';
end
if k == 3
k = '3';
elseif k == 4
k = '4';
else k == 6
k = '6';
end
load([i '_P0009_S00' k '_Int_Right.mat']);
a(j) = AnkleAng_X;
I also tried using string manipulation to change the variable name everytime it loops but that also does not work
for i = 1:2
for k = [3,4,6]
if i == 1
i = 'Ground';
else
i = 'Sand';
end
if k == 3
k = '3';
elseif k == 4
k = '4';
else k == 6
k = '6';
end
load([i '_P0009_S00' k '_Int_Right.mat']);
[i '_ankle_x_' k] = AnkleAng_X;
Does anyone have a solution to this problem?

Answers (1)

Image Analyst
Image Analyst on 14 Dec 2014
What kind of variable is AnkleAng_X? Is it a scalar? A string? A vector or matrix or higher dimension array? How about if you just load it into a higher dimension array? Like if it's a row vector, just stuff it into a new row in a 2D matrix that you build up row by row?
By the way, you can use sprintf() and fullfile() to build up a filename:
baseFileName = sprintf('%d_P0009_S00%d_Int_Right.mat', i, k);
fullFileName = fullfile(folder, baseFileName);
if exist(fullFileName, 'file')
thisStructure = load(fullFileName);
a{j} = thisStructure.AnkleAng_X;
end

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!