필터 지우기
필터 지우기

Loop for increment number in file name

조회 수: 34 (최근 30일)
Francis Trombini
Francis Trombini 2022년 11월 12일
댓글: Voss 2022년 11월 15일
I need to save 10 files "AccelGyro_Gait" with the suffix from 01 to 10, i.e., AccelGyro_Gait_01, AccelGyro_Gait_02...
This is my starting code. But I'm not able to finish it efficiently. Only the last trial
for i = 1:10
run rawP5reader; % Load files
gyro = (ans(2).data);
accel = (ans(1).data(1:length(gyro),:));
timeStamps = (ans(2).timestamps);
zerosMat = zeros(length(timeStamps),7);
zerosMat(:,1) = (timeStamps);
zerosMat(:,2:4) = accel;
zerosMat(:,5:7) = gyro;
AccelGyro_Gait = zerosMat;
% .... Save the file name by adding numbers at the end.
end

채택된 답변

Image Analyst
Image Analyst 2022년 11월 12일
Try this:
outputFolder = pwd; % Or 'C:\whatever'
for i = 1:10
run rawP5reader; % Load files
gyro = (ans(2).data);
accel = (ans(1).data(1:length(gyro),:));
timeStamps = (ans(2).timestamps);
zerosMat = zeros(length(timeStamps),7);
zerosMat(:,1) = (timeStamps);
zerosMat(:,2:4) = accel;
zerosMat(:,5:7) = gyro;
AccelGyro_Gait = zerosMat;
% Save the file name by adding numbers at the end.
baseFileName = sprintf('AccelGyro_Gait_%2.2d.mat', i);
fullFileName = fullfile(outputFolder, baseFileName);
fprintf('Saving "%s".\n', fullFileName);
save(fullFileName, 'AccelGyro_Gait');
end
  댓글 수: 1
Francis Trombini
Francis Trombini 2022년 11월 13일
Thanks for your detailed answer, Image Analyst. It helped me a lot.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Voss
Voss 2022년 11월 12일
Here's one way to generate those file names:
for i = 1:10
current_file_name = sprintf('AccelGyro_Gait_%02d',i)
% then do something with current_file_name,
% e.g., use it as the file to save something to
end
current_file_name = 'AccelGyro_Gait_01'
current_file_name = 'AccelGyro_Gait_02'
current_file_name = 'AccelGyro_Gait_03'
current_file_name = 'AccelGyro_Gait_04'
current_file_name = 'AccelGyro_Gait_05'
current_file_name = 'AccelGyro_Gait_06'
current_file_name = 'AccelGyro_Gait_07'
current_file_name = 'AccelGyro_Gait_08'
current_file_name = 'AccelGyro_Gait_09'
current_file_name = 'AccelGyro_Gait_10'
  댓글 수: 2
Francis Trombini
Francis Trombini 2022년 11월 13일
Thanks for your answer. I was able to develop some solutions based on your information.
Voss
Voss 2022년 11월 15일
You're welcome! Any questions, let me know. Otherwise, please "Accept This Answer". Thanks!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by