Help using rowfun?

16 ビュー (過去 30 日間)
Brooke Sarley
Brooke Sarley 2016 年 3 月 18 日
コメント済み: Brooke Sarley 2016 年 3 月 19 日
I'm trying to use rowfun on an 18x2 table created here:
peaks= table(rot90(locs,3),pks,'VariableNames',{'d' 'int'});
I'm trying to apply the following function (titled intovol) after defining lambda and energy earlier in the script
function [ phase, chemeq, volume ] = intovol(lambda,energy,d,int)
using rowfun:
volumes = rowfun(intovol(lambda,energy,d,int),peaks);
Together in the script they look like this:
peaks= table(rot90(locs,3),pks,'VariableNames',{'d' 'int'});
volumes = rowfun(intovol(lambda,energy,d,int),peaks);
But I keep getting the error "Undefined function or variable 'd'."
How do I get row fun to assign the values of the first and second columns of the table to variables in the function? I've tried using
volumes = rowfun(intovol(lambda,energy,d,int),peaks,'InputVariables',{'d' 'int'})
but it didn't work

回答 (1 件)

Steven Lord
Steven Lord 2016 年 3 月 18 日
The ROWFUN function requires as its first input a function handle to a function to be executed for each row in the table. See how all the examples on that page pass function handles (which could be an anonymous function) in as the first input? You're not passing a function handle as the first input in your call; you're instead calling a function and passing whatever it returns as the first input. This could work if what you're calling returned a function handle, but it refers to a variable that doesn't exist.
Read through the examples on that page of how to specify a function handle and how to tell ROWFUN which variables in the table are inputs and which control how the rows are grouped.
  3 件のコメント
Stephen23
Stephen23 2016 年 3 月 18 日
編集済み: Stephen23 2016 年 3 月 18 日
@Brooke Sarley: you can create another function handle based on the first one, that specifies the values of lambda and energy:
@(d,i)intovol(lambda,energy,d,i)
See the documentation on anonymous functions.
Brooke Sarley
Brooke Sarley 2016 年 3 月 19 日
Thank you Stephen. I'd accept the answer but you answered it, not Steven.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by