[FieldTrip] Several variables as input into function without writing them manually?

Erika Puiutta erika.puiutta at uni-oldenburg.de
Mon Jul 22 10:50:09 CEST 2019


Hey Jan-Mathijs,


thanks a lot! Good to know that I need cell instead of structures for sthg. like that. Maybe I will change my bookkeeping in the future ;)


Kind regards,

Erika

________________________________
Von: fieldtrip <fieldtrip-bounces at science.ru.nl> im Auftrag von Schoffelen, J.M. (Jan Mathijs) <jan.schoffelen at donders.ru.nl>
Gesendet: Dienstag, 16. Juli 2019 16:14:29
An: FieldTrip discussion list
Betreff: Re: [FieldTrip] Several variables as input into function without writing them manually?

Hi Erika,

You could do the following:

tmp = cell(1,numel(s));
for k = 1:numel(s)
  tmp{k} = s(k).rawdata.data00;
end
appendeddata = ft_appenddata(cfg, tmp{:});

In short, a varargin type of argument needs to be represented as a (curly bracket expanded) cell-array. Struct-arrays (as in your case) may be easy to work with in other situations (e.g. when you quickly want to concatenate fields from different structures) , but indeed not so much in a situation where you want to flexibly input multiple arguments into a function. Your use of nested struct arrays is perhaps handy in terms of bookkeeping, but makes concise coding somewhat complicated (not that it’s wrong, but I just want to have it mentioned :) ).

I hope this helps,

Jan-Mathijs

On 16 Jul 2019, at 12:06, Erika Puiutta <erika.puiutta at uni-oldenburg.de<mailto:erika.puiutta at uni-oldenburg.de>> wrote:

Hello!

I am analysing MEG data and have 20 participants, so 20 datasets. I want to append all these with ft_appenddata.
Is there a way to plug in all the variable names without having to manually spell out all of them?
For example:
[appendeddata] = ft_appenddata(cfg, s(1).rawdata.data00,s(2).rawdata.data00, s(3).rawdata.data00,... and so forth

So I would have to spell out all 20 names which is quite cumbersome. I tried to get around this by using sprinft like this:

k=strings(1,20);
k(1:20)=1:20;
cfg=[];
[appendeddata] = ft_appenddata(cfg,sprintf('s(%s).rawdata.data00,',k));


But if I do this, I get these errors:
Error using ft_checkdata (line 529)
This function requires 'raw' or 'raw+comp' data as input, see ft_datatype_raw.

Error in ft_appenddata (line 102)
  varargin{i} = ft_checkdata(varargin{i}, 'datatype', {'raw', 'raw+comp'}, 'feedback', 'no', 'hassampleinfo', cfg.keepsampleinfo);

So I guess using sprintf doesn't work. Is there another way to put in several variables into ft_appenddata (or any other fieldtrip function) without having to write them down myself?

Thanks for your answers in advance!

Kind regards
Erika
_______________________________________________
fieldtrip mailing list
https://mailman.science.ru.nl/mailman/listinfo/fieldtrip
https://doi.org/10.1371/journal.pcbi.1002202

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.science.ru.nl/pipermail/fieldtrip/attachments/20190722/00638d57/attachment-0002.html>
-------------- next part --------------
_______________________________________________
fieldtrip mailing list
https://mailman.science.ru.nl/mailman/listinfo/fieldtrip
https://doi.org/10.1371/journal.pcbi.1002202


More information about the fieldtrip mailing list