csvread Loop Input with Changing String Name
2 ビュー (過去 30 日間)
古いコメントを表示
I am trying to read in several files that change depending on a) name, and b) task. An example file is:
'resp_signal_ben_15_brpm.csv'
Other files are the same but have either the name (ben) or the task (15 brpm) changed. I tried to make a few arrays containing the changing parameters, i.e.:
task = ['6_brpm','10_brpm','15_brpm','24_brpm','hold_breath','silent_talk']
name = ['ben','joe','bob']
Then create a nested loop that would insert the appropriate name and task:
for i = 1:3
for j = 1:6
a = csvread('resp_signal_',name(i),'_',task(j),'.csv');
subplot(2,3,i);
plot(a(:,2));
end
end
However, this does not work and I'm sure it probably has to do with the way csvread is taking in these arguments. Can anyone help me with an iterative way read in these files correctly?
1 件のコメント
per isakson
2015 年 6 月 17 日
Try replace
a = csvread('resp_signal_',name(i),'_',task(j),'.csv');
by
a = csvread(['resp_signal_',name(i),'_',task(j),'.csv']);
採用された回答
Guillaume
2015 年 6 月 17 日
編集済み: Guillaume
2015 年 6 月 17 日
The way you're constructing task and name, you're just making one long string by concatenating smaller ones, that is the end result is:
task = '6_brpm10_brpm15_brpm24_brpmhold_breathsilent_talk'
name = 'benjoebob'
So, change number one is to construct them as cell arrays of strings instead:
task = {'6_brpm','10_brpm','15_brpm','24_brpm','hold_breath','silent_talk'};
name = {'ben','joe','bob'};
the curly brackets are critical.
Secondly, the way to concatenate strings is not by piling them up as arguments of the function (csvread or any other), but by concatenating them first and then passing the concatenation as a single argument to the function. You can do the concatenation by using []:
a = csvread(['resp_signal_', name{i}, '_', task{j}, '.csv']) %note that the curly braces are again critical
Personally, I prefer using sprintf for making the string:
a = csvread(sprintf('resp_signal_%s_%s.csv', name{i}, task{j}))
Finally, I wouldn't hardcode the end of the loop, but would get the value from the size of the arrays.That way, if you add another task, you only have to change the array:
for i = 1:numel(task) %and I would use t instead of i
for j = 1:numel(name) %and I would use n instead of j
2 件のコメント
Guillaume
2015 年 6 月 18 日
Conceptually, a structure is a cell array where you give a name to each cell. So instead of indexing you use the name to refer to a particular cell. Another container for different data type is the table, where you can use both indexing and name to access data.
To access data in a structure, you use dot notation: s.fieldname
To access data in a cell, you use curly brackets: c{index}
To access data in a table, you can use either: t.colname(row), t{row, col}, or t{row, 'colname'}
In your particular case, I would stay with a cell array. It would make no sense to name each cell. It's more complicated to iterate over the fields of a structure. The overhead of a table wouldn't be worth it here.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Cell Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!