changing matrix names in loops

조회 수: 1 (최근 30일)
Leelai
Leelai 2014년 12월 14일
답변: Image Analyst 2014년 12월 14일
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?

답변 (1개)

Image Analyst
Image Analyst 2014년 12월 14일
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

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by