how to programmatically define parameters and assign values - Сообщения
I would like to build a function that will extract a row of data from a matrix and assign each columns to a named parameter. Basically I want a vlookup function + paramters assignment.
I got the vlookup part working quite well (for my needs), but I'm unable to assign programmatically the extracted value to the proper variable. Here an example:
example.sm (14 КиБ) скачан 31 раз(а).
As you can see the vlookup function (1) works (I know I'm cheating a little bit because it works based on row index and not on a condition being checked, but it is fine for now), and also the manual assignment (2) works as intended, but I would like to automate that so that I just need to invoke the function to update the parameters.
I've tried to put the assignment ( "a:=..." ) inside the function, but does not work (I guess something to do with local and global variables?).
My final goal is to build a library of equations to be included() as needed, and then invoke functions to set up the parameters to be actually used in the worksheet, without the need to rewriting or copy and pasting the same code for each worksheet.
BONUS: if it would be possible to read the parameter name from the header and then assign it the proper value, it would be sweet: let's say the header is '"var.1"', then the function will create 'var.1:=...' without the double quotation mark ( " ).
Am I out of my mind?
Hope that helps.
Best regards.
Alvaro.
WroteMy final goal is to build a library of equations to be included() as needed, and then invoke functions to set up the parameters to be actually used in the worksheet, without the need to rewriting or copy and pasting the same code for each worksheet.
Include works charming well if done right.
Download both in same Smath session ... explore two different fits.
For the remaining of your visit ... very many questions.
Please, don't hesitate for more ... Jean
Genfit Minimize Include.sm (31 КиБ) скачан 37 раз(а).
example.sm (35 КиБ) скачан 34 раз(а).
Genfit Minimize Include Test.sm (58 КиБ) скачан 33 раз(а).
WroteHi. As far as I know, there are two methods to modify a variable from a SMath function. One is modifying in the body of the function one of the parameters that has been passed, which must be done explicitly. That is, f (a, x) can only modify variables x or a, but not others. The second way is to create a string that assigns any variable and executing that string in the scope that you need that assignment, that is, global from the worksheet or local if you do it within a function.
Hope that helps.
Best regards.
Alvaro.
Ah I didn't expect the num2str()/str2num() to work that way!
Following your example I written a function that will take a key, value pair and assign the value to the proper key, but the line() thing is throwing me off a little bit: if I use it, then declared variables would be local, else if I don't use it the function will throw an error and will not work.
Best I can do is a 2 functions approach:
- the first will extract a vector of [key, value] pairs
- the second will declare the variables using the vectorize function
Ideally I would like this two functions to be wrapped in only one, but I am not able to.
example.sm (28 КиБ) скачан 30 раз(а).
For the vectorized case you need to ensure that what goes to be vectorized is evaluated, if not vectorize function try to do its task to the argument of declare_var too. One workaround is store the argument of declare_var in one temp variable
Your for loop version is correct to, but you need to let the loop "free" at the worksheet or, if you want to assign it a variable like f, as you do, you need to end the loop with some exit value
Best regards.
Alvaro.
WroteHi. Your worksheet have only a couple of non related issues.
For the vectorized case you need to ensure that what goes to be vectorized is evaluated, if not vectorize function try to do its task to the argument of declare_var too. One workaround is store the argument of declare_var in one temp variable
I did this in the first page of the worksheet, and then I tried to combine in one go without success.
Цитата
Your for loop version is correct to, but you need to let the loop "free" at the worksheet or, if you want to assign it a variable like f, as you do, you need to end the loop with some exit value
Best regards.
Alvaro.
even if I let the for-loop "free" at the worksheet, it will work, but I still get an error ( "there is no result" ):
the "=" in this case is needed to trigger the evaluation of the for-loop. Without it, it wouldn't do anything. In my previous version I tried instead the "f:=for(...)" because then the for-loop would get evaluated, but if I put the for-loop in the line() as in your example, I "entrap" the "declare_vars()" in the local scope and would not define the variables in the global scope.
As of now, I see no other solution than to use the two functions approach with the temporary variable to pass to declare_var().
Wrote... I did this in the first page of the worksheet, and then I tried to combine in one go without success. ...
You're right, I don't see that. BTW I think that from the view point of SMath, this is a only one instruction, because line:
Wrote
even if I let the for-loop "free" at the worksheet, it will work, but I still get an error ( "there is no result" ):
...
the "=" in this case is needed to trigger the evaluation of the for-loop. Without it, it wouldn't do anything. In my previous version I tried instead the "f:=for(...)" because then the for-loop would get evaluated, but if I put the for-loop in the line() as in your example, I "entrap" the "declare_vars()" in the local scope and would not define the variables in the global scope.
You can 'force' or 'trigger' or or whatever it takes for the loops to realize that it exists and matters assigning a dummy variable in the loop body
Wrote
As of now, I see no other solution than to use the two functions approach with the temporary variable to pass to declare_var().
I guess that this is another approach wich can work and it is which I use
example.sm (34 КиБ) скачан 51 раз(а).
Best regards.
Alvaro.
-
Новые сообщения
-
Нет новых сообщений