Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

How to use cellfun with a function that has multiple arguments?

1 vue (au cours des 30 derniers jours)
lil brain
lil brain le 26 Jan 2022
Clôturé : Stephen23 le 26 Jan 2022
Hi,
I have the function dfaedit_2 which takes three arguments:
H = dfaedit_2(0,0,0)
Now, I want to run this fucntion on every cell in my cell array p_windows.mat using
cellfun(@dfaedit_2, C).
My question is how would I write this to input all the arguments needed for dfaedit_2?
Thank you!
  1 commentaire
Stephen23
Stephen23 le 26 Jan 2022
Modifié(e) : Stephen23 le 26 Jan 2022
"I have the function dfaedit_2 which takes three arguments: "
Actually your MAIN function takes no arguments at all. These are the first six lines of your file:
function main
file_name = 'p_windows.mat';
H = dfaedit(file_name,1,1,1)
end
function [H]=dfaedit(file_name,plot_flag, outfile_flag, out_command_flag)
...
"I want to run this fucntion on every cell in my cell array p_windows.mat"
A cell array is an array in the MATLAB workspace. A .mat file is a binary filed saved on a harddrive. Not the same thing.
"How to use cellfun with a function that has multiple arguments?"
Simpy ensure that you provide the function with its required inputs, e.g.:
fnh = @(a,b) sprintf('%s %s',a,b);
C1 = {'cat','hello'};
C2 = {'hat','world'};
cellfun(fnh,C1,C2,'uni',0)
ans = 1×2 cell array
{'cat hat'} {'hello world'}

Réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by