Help with rowfun?

I've created a table with columns Go,Export, and Directory and row names from a list of strings (ex {'First Row','Second Row'})
I have a helper function, generic_helper that takes each of a Go, Export, and Directory values from a table, along with the string corresponding to the row (ex 'First Row') and a binary flag that does not change across rows.
How can I apply rowfun to call generic_helper(Go,Export,Directory,rowName,ex_bin_flag) for the values of Go,Export,Directory, and rowName in each row of my table?
Thanks in advance!

2 个评论

Have you considered a for-loop? If so, why doesn't that do what you need?
I have one now, but as I understand with tables in matlab vectorizing is particularly advantageous in terms of speed (unlike say cellfun for cell arrays). if I'm mistaken, I'd love an explanation!

请先登录,再进行评论。

回答(1 个)

Rik
Rik 2017-2-22
You should have the helper function with some output (one or multiple scalars (single values)). Then, in your code you can use
newtable=rowfun(@generic_helper,table);
Note from rowfunc doc: "To return more than one output from func, use the 'NumOutputs' or 'OutputVariableNames' name-value pair arguments."
PS you were correct in looking for a vectorized solution, they are indeed generally faster. For smaller problems the effect is often negligible, but it always affects readability.

10 个评论

amen45
amen45 2017-2-22
编辑:amen45 2017-2-22
Thank you! Do you have guidance on how to incorporate the outside binary flag and the row name? That was the part that confused me in the documentation
How do you need to incorporate them? If you need multiple outputs in newtable, just try something like
newtable=rowfun(@generix_helper,table,'OutputVariableNames',{'CalculatedValue','bin_flag','RowName'});
Sorry, I think I asked the question poorly. Behavior within generic_helper is dictated by the RowName and the binary flag. The binary flag turns off user warnings, so it's constant across all rows, as is a src directory string I forgot to include above. Does that make sense?
For example, my for loop right now looks something like this:
rnames = table.Properties.RowNames;
for curr_row = 1:length(rnames)
new_table{rnames{curr_row},'Directory'} = generic_helper (table{rnames{curr_row},'Go'},...
table{rnames{curr_row},'Export'},table{rnames{curr_row},'Directory'}, ...
src_dir_string,rnames{curr_row}, warning_flag_off);
end
Thanks for your help with this!
I was not aware that you could use a string as row-index and still get your intended behavior. As long as you make sure that the table you feed to rowfun has as many columns as generic_helper has inputs, the line in the main answer body should work.
amen45
amen45 2017-2-23
编辑:amen45 2017-2-23
yes, but the reason I need help is that I have 2 pieces of information that are not in the table, and I don't want to include them in the table, since they apply to all the rows in the same way. Is this not possible?
It is possible with a global variable. Those should generally be avoided, but I don't see another solution right now. What is the harm in creating a temporary table that does include those pieces of information?
Right now these are global variables, because I need them elsewhere. Does that help? I can try creating a temporary table if necessary, or just keep the for loop, if this isn't possible.
Global variables are best avoided, because they make code difficult to debug and can be unpredictable if edited somewhere unexpected. You can just call global bin_flag rowName inside the helper function and it should work.
sorry, should have been more specific. they are globals that are passed to the generic helper function, as shown in my example code above. This is all happening in a larger module.
Well, then you can either declare bin_flag and rowName as global variables, or create two new global variables in which you paste the content of bin_flag and rowName.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Logical 的更多信息

标签

提问:

2017-2-21

评论:

Rik
2017-2-24

Community Treasure Hunt

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

Start Hunting!

Translated by