csvread Loop Input with Changing String Name

2 ビュー (過去 30 日間)
bmeyer
bmeyer 2015 年 6 月 17 日
コメント済み: Guillaume 2015 年 6 月 18 日
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
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
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 件のコメント
bmeyer
bmeyer 2015 年 6 月 17 日
Thanks! That helps a ton. One more question - what is the difference between cell arrays and structures? Is there one? Maybe MATLAB handles them differently, but from my experience with other languages (I think I may be specifically confusing it with python here) creating a struct uses the same notation (curly brackets) while creating a cell array uses regular brackets.
Guillaume
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 ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by