Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
Offline kompre  
#1 Posted : 25 November 2021 16:19:27(UTC)
kompre


Rank: Member

Groups: Registered
Joined: 05/08/2021(UTC)
Posts: 19
Italy

I am little bit at loss.

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:

2021-11-25 13_59_30-SMath Solver - [example.sm].png
example.sm (15kb) downloaded 5 time(s).

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?

Edited by user 25 November 2021 16:21:39(UTC)  | Reason: Not specified

Offline Razonar  
#2 Posted : 25 November 2021 18:02:47(UTC)
Razonar


Rank: Advanced Member

Groups: Registered
Joined: 28/08/2014(UTC)
Posts: 872
Uruguay

Was thanked: 474 time(s) in 307 post(s)
Hi. 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.

img0.png

Hope that helps.
Best regards.
Alvaro.
Offline Jean Giraud  
#3 Posted : 25 November 2021 19:12:52(UTC)
Jean Giraud


Rank: Advanced Member

Groups: Registered
Joined: 04/07/2015(UTC)
Posts: 5,800
Canada

Was thanked: 867 time(s) in 697 post(s)
Originally Posted by: kompre Go to Quoted Post
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.

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 (32kb) downloaded 9 time(s).
example.sm (36kb) downloaded 10 time(s).
Genfit Minimize Include Test.sm (59kb) downloaded 7 time(s).

Offline kompre  
#4 Posted : 26 November 2021 11:33:20(UTC)
kompre


Rank: Member

Groups: Registered
Joined: 05/08/2021(UTC)
Posts: 19
Italy

Originally Posted by: Razonar Go to Quoted Post
Hi. 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.

img0.png

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:

  1. the first will extract a vector of [key, value] pairs
  2. 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 (29kb) downloaded 5 time(s).
Offline Razonar  
#5 Posted : 26 November 2021 12:01:06(UTC)
Razonar


Rank: Advanced Member

Groups: Registered
Joined: 28/08/2014(UTC)
Posts: 872
Uruguay

Was thanked: 474 time(s) in 307 post(s)
Hi. 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

img1.png

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

img2.png

img3.png

Best regards.
Alvaro.
Offline kompre  
#6 Posted : 26 November 2021 14:10:08(UTC)
kompre


Rank: Member

Groups: Registered
Joined: 05/08/2021(UTC)
Posts: 19
Italy

Originally Posted by: Razonar Go to Quoted Post
Hi. 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


img1.png



I did this in the first page of the worksheet, and then I tried to combine in one go without success.

2021-11-26 11_47_17-SMath Solver - [example (2).sm_].png

Quote:


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

img2.png

img3.png

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" ):

2021-11-26 11_57_24-SMath Solver - [example (2).sm_].png

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().




Offline Razonar  
#7 Posted : 26 November 2021 18:59:43(UTC)
Razonar


Rank: Advanced Member

Groups: Registered
Joined: 28/08/2014(UTC)
Posts: 872
Uruguay

Was thanked: 474 time(s) in 307 post(s)
Originally Posted by: kompre Go to Quoted Post
... 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:

img1.png


Originally Posted by: kompre Go to Quoted Post


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

img2.png

Originally Posted by: kompre Go to Quoted Post


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

img3.png

example.sm (35kb) downloaded 9 time(s).

Best regards.
Alvaro.

Edited by user 26 November 2021 19:03:38(UTC)  | Reason: Not specified

Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.