Help with rowfun?
Mostrar comentarios más antiguos
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!
Respuestas (1)
Rik
el 22 de Feb. de 2017
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 comentarios
Rik
el 22 de Feb. de 2017
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'});
Rik
el 22 de Feb. de 2017
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.
Rik
el 23 de Feb. de 2017
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?
amen45
el 23 de Feb. de 2017
Rik
el 23 de Feb. de 2017
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.
amen45
el 24 de Feb. de 2017
Rik
el 24 de Feb. de 2017
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.
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!