csvread Loop Input with Changing String Name
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
bmeyer
el 17 de Jun. de 2015
Comentada: Guillaume
el 18 de Jun. de 2015
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 comentario
per isakson
el 17 de Jun. de 2015
Try replace
a = csvread('resp_signal_',name(i),'_',task(j),'.csv');
by
a = csvread(['resp_signal_',name(i),'_',task(j),'.csv']);
Respuesta aceptada
Guillaume
el 17 de Jun. de 2015
Editada: Guillaume
el 17 de Jun. de 2015
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 comentarios
Guillaume
el 18 de Jun. de 2015
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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Cell Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!