How can I import this data with a for loop?

11 ビュー (過去 30 日間)
Lucas
Lucas 2023 年 12 月 5 日
コメント済み: Matt J 2023 年 12 月 5 日
Data3 = importdata('PendulumTestData/TD3/IK/HR_1.mot')
Data5 = importdata('PendulumTestData/TD5/IK/HR_1.mot')
Data7 = importdata('PendulumTestData/TD7/IK/HR_1.mot')
Data9 = importdata('PendulumTestData/TD9/IK/HR_1.mot')
Data11 = importdata('PendulumTestData/TD11/IK/HR_1.mot')
Data12 = importdata('PendulumTestData/TD12/IK/HR_1.mot')
Data14 = importdata('PendulumTestData/TD14/IK/HR_1.mot')
Data15 = importdata('PendulumTestData/TD15/IK/HR_1.mot')
Data16 = importdata('PendulumTestData/TD16/IK/HR_1.mot')
Data17 = importdata('PendulumTestData/TD17/IK/HR_1.mot')
x = [3 5 7 9 11 12 14 15 16 17];
for i = 1:10
Data{i} = importdata(sprintf('PendulumTestData/TD%.1f/IK/HR_1.mot', x(i)));
end
I tried something, but it doesn't work...

回答 (2 件)

Matt J
Matt J 2023 年 12 月 5 日
Data{i} = importdata(compose("PendulumTestData/TD%d/IK/HR_1.mot", x(i)));

Les Beckham
Les Beckham 2023 年 12 月 5 日
You were very close.
x = [3 5 7 9 11 12 14 15 16 17];
for i = 1:numel(x)
fn = sprintf('PendulumTestData/TD%d/IK/HR_1.mot', x(i))
% ^ use %d here
% Data{i} = importdata(fn); % commented out so it will run here
end
fn = 'PendulumTestData/TD3/IK/HR_1.mot'
fn = 'PendulumTestData/TD5/IK/HR_1.mot'
fn = 'PendulumTestData/TD7/IK/HR_1.mot'
fn = 'PendulumTestData/TD9/IK/HR_1.mot'
fn = 'PendulumTestData/TD11/IK/HR_1.mot'
fn = 'PendulumTestData/TD12/IK/HR_1.mot'
fn = 'PendulumTestData/TD14/IK/HR_1.mot'
fn = 'PendulumTestData/TD15/IK/HR_1.mot'
fn = 'PendulumTestData/TD16/IK/HR_1.mot'
fn = 'PendulumTestData/TD17/IK/HR_1.mot'
  2 件のコメント
Lucas
Lucas 2023 年 12 月 5 日
Thank you!!
Matt J
Matt J 2023 年 12 月 5 日
@Lucas If your problem is solved, please accept-click the most appropriate answer.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by