How to Simplify Passing Multiple Arguments from Structs in MATLAB Function Calls?
am working on a MATLAB project where I need to pass multiple arguments from two structs (n and d) to a function called mi_decode. Currently, I am passing each field individually, which makes the code quite verbose and hard to maintain. Here is a snippet of my current code:
function results = spectral_mi(labels, data, n, d)
d.pl_ u int8
d.prr_k cell
d.is_cnrm logical = true
d.is_chk logical = true
d.is_fbk logical = true
d.is_x logical = false
d.is_pl logical = false
d.is_prr logical = false
n.cl uint16 = numel(unique(labels))
n.t uint8 = size(data{1}, 2)
n.k uint8 = numel(data)
n.trials uint8 = numel(labels)
n.f uint8 = fix(size(data{1}, 2)/2)+1
n.pl uint16
% … other code …
pow_per_f = cell(1, n.f);
phase_per_f = cell(1, n.f);
for f = 1:n.f
pow_per_f{f} = squeeze(power(abs(st_data(:, f, :)), 2));
phase_per_f{f} = squeeze(angle(st_data(:, f, :)));
end
[mi_, zmi_, pmi_] = mi_decode( …
labels, pow_per_f{:}, …
"is_x", d.is_x, …
"t", n.t, …
"k", n.f, …
"pl", n.pl, …
"is_cnrm", d.is_cnrm, …
"is_chk", d.is_chk, …
"is_fbk", d.is_fbk, …
"is_pl", d.is_pl, …
"is_prr", d.is_prr);
[phase_mi_, phase_zmi_, phase_pmi_] = mi_decode( …
labels, phase_per_f{:}, …
"is_x", d.is_x, …
"t", n.t, …
"k", n.f, …
"pl", n.pl, …
"is_cnrm", d.is_cnrm, …
"is_chk", d.is_chk, …
"is_fbk", d.is_fbk, …
"is_pl", d.is_pl, …
"is_prr", d.is_prr);
end
Is there a more concise way to pass all the fields from the structs n and d to the mi_decode function without listing each field individually? Any suggestions or best practices for handling this in MATLAB would be greatly appreciated.am working on a MATLAB project where I need to pass multiple arguments from two structs (n and d) to a function called mi_decode. Currently, I am passing each field individually, which makes the code quite verbose and hard to maintain. Here is a snippet of my current code:
function results = spectral_mi(labels, data, n, d)
d.pl_ u int8
d.prr_k cell
d.is_cnrm logical = true
d.is_chk logical = true
d.is_fbk logical = true
d.is_x logical = false
d.is_pl logical = false
d.is_prr logical = false
n.cl uint16 = numel(unique(labels))
n.t uint8 = size(data{1}, 2)
n.k uint8 = numel(data)
n.trials uint8 = numel(labels)
n.f uint8 = fix(size(data{1}, 2)/2)+1
n.pl uint16
% … other code …
pow_per_f = cell(1, n.f);
phase_per_f = cell(1, n.f);
for f = 1:n.f
pow_per_f{f} = squeeze(power(abs(st_data(:, f, :)), 2));
phase_per_f{f} = squeeze(angle(st_data(:, f, :)));
end
[mi_, zmi_, pmi_] = mi_decode( …
labels, pow_per_f{:}, …
"is_x", d.is_x, …
"t", n.t, …
"k", n.f, …
"pl", n.pl, …
"is_cnrm", d.is_cnrm, …
"is_chk", d.is_chk, …
"is_fbk", d.is_fbk, …
"is_pl", d.is_pl, …
"is_prr", d.is_prr);
[phase_mi_, phase_zmi_, phase_pmi_] = mi_decode( …
labels, phase_per_f{:}, …
"is_x", d.is_x, …
"t", n.t, …
"k", n.f, …
"pl", n.pl, …
"is_cnrm", d.is_cnrm, …
"is_chk", d.is_chk, …
"is_fbk", d.is_fbk, …
"is_pl", d.is_pl, …
"is_prr", d.is_prr);
end
Is there a more concise way to pass all the fields from the structs n and d to the mi_decode function without listing each field individually? Any suggestions or best practices for handling this in MATLAB would be greatly appreciated. am working on a MATLAB project where I need to pass multiple arguments from two structs (n and d) to a function called mi_decode. Currently, I am passing each field individually, which makes the code quite verbose and hard to maintain. Here is a snippet of my current code:
function results = spectral_mi(labels, data, n, d)
d.pl_ u int8
d.prr_k cell
d.is_cnrm logical = true
d.is_chk logical = true
d.is_fbk logical = true
d.is_x logical = false
d.is_pl logical = false
d.is_prr logical = false
n.cl uint16 = numel(unique(labels))
n.t uint8 = size(data{1}, 2)
n.k uint8 = numel(data)
n.trials uint8 = numel(labels)
n.f uint8 = fix(size(data{1}, 2)/2)+1
n.pl uint16
% … other code …
pow_per_f = cell(1, n.f);
phase_per_f = cell(1, n.f);
for f = 1:n.f
pow_per_f{f} = squeeze(power(abs(st_data(:, f, :)), 2));
phase_per_f{f} = squeeze(angle(st_data(:, f, :)));
end
[mi_, zmi_, pmi_] = mi_decode( …
labels, pow_per_f{:}, …
"is_x", d.is_x, …
"t", n.t, …
"k", n.f, …
"pl", n.pl, …
"is_cnrm", d.is_cnrm, …
"is_chk", d.is_chk, …
"is_fbk", d.is_fbk, …
"is_pl", d.is_pl, …
"is_prr", d.is_prr);
[phase_mi_, phase_zmi_, phase_pmi_] = mi_decode( …
labels, phase_per_f{:}, …
"is_x", d.is_x, …
"t", n.t, …
"k", n.f, …
"pl", n.pl, …
"is_cnrm", d.is_cnrm, …
"is_chk", d.is_chk, …
"is_fbk", d.is_fbk, …
"is_pl", d.is_pl, …
"is_prr", d.is_prr);
end
Is there a more concise way to pass all the fields from the structs n and d to the mi_decode function without listing each field individually? Any suggestions or best practices for handling this in MATLAB would be greatly appreciated. struct, functions, arguments MATLAB Answers — New Questions