Info

此问题已关闭。 请重新打开它进行编辑或回答。

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

1 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2022-1-26
编辑:Stephen23 2022-1-26
"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'}

回答(0 个)

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by