如何通过将其名称作为字符串来使用函数

时间:2017-01-22 21:21:19

标签: string matlab function eval sequence

假设您要使用MATLAB中定义的一系列函数,并且您只需将这些函数的名称作为字符串变量。假设您已经创建了 mysqli_query($con,$sql) or die((mysql_error()); fun1,...,fun2,并且您还有一个字符串向量funN。如何自动调用每个函数而不必逐个写入每个函数的名称?

1 个答案:

答案 0 :(得分:6)

使用//get the buttons and add an event listner for click on the calculate button var buttons = document.getElementsByTagName("button"); var calculateButton = buttons[2]; //get the total element to add to later var total = document.querySelector('.total > span'); //items to iterate over var itemRows = document.querySelectorAll('.item-row'); calculateButton.addEventListener('click', updatePriceByProduct); function calculateTotals(itemRow) { //get the item's price and quantity var price = itemRow.children[1].innerHTML; var quantity = itemRow.children[2].children[0].value; var total = price * quantity; //set item's total to its total itemRow.children[3].innerHTML = total; return total; } function calculateTotal(totals) { var newTotal = totals.reduce(function(a,b) {return a + b}); console.log(newTotal, 'new total'); total.innerHTML = newTotal; } function updatePriceByProduct(event){ event.preventDefault(); //return an array with the totals from each item-row var prices = Array.prototype.map.call(itemRows, calculateTotals); //calcualte the total from the array of prices calculateTotal(prices); } 。当然,如果函数已被定义为函数句柄(例如str2func),则可以跳过下面的str2func步骤。

fun1 = @(x)x+x.^2+sqrt(x))