Given that I have a matrix of variable name strings and the respective values in another matrix (both come from a csv file), how can I create variables in the workspace that have the names from the name matrix and the values from the value matrix?
I have found global to define a variable's scope so that I can write to it in a function, but I haven't found a way to handle runtime variable names.
You should use execstr function (see: https://help.scilab.org/docs/5.5.2/en_US/execstr.html)
For example, with a matrix names stored in the variable MatrixNames and the matrix content stored in the variable MatrixContent, you will simply have:
execstr(MatrixName(i)+'= MatrixContent');
With i the cell number for the corresponding matrix name you want to treat.
As #david-dorchies suggested, you should use execstr. To make sure they are globally accesible use globals if you want to do it in a function.
See below for an example implementation.
funcprot(0);
clear;
function assign_to_globals(names, values)
for i=1:length(values)
execstr(sprintf('clearglobal %s; global %s;', names(i), names(i)))
execstr(sprintf('%s = %s;', names(i), string(values(i))))
end;
endfunction
function disp_all_globals(names)
for i=1:(size(names,1)*size(names,2))
disp(names(i))
execstr(sprintf('global %s; disp(%s)', names(i), names(i)))
end;
endfunction
values = list(23,5.6,6/10,"[1,2,3]");
names = ['a','my_long_var_name','c1','my_sub_mat'];
assign_to_globals(names, values)
disp_all_globals(names)
clearglobal()
Related
How do I create a custom function which takes an integer input, and uses that integer input as a parameter to another function inside it, and returns a variable which appends the integer to its name?
For example:
f <- function(i) {
var_i<-another.Function(parameter="ni")
}
This should return the variable var_1 if I pass i=1.
My goal is to implement parallel processing using this function which I would pass a range of integers for processes, i.e mclapply(1:13,f)
I initially used eval(parse=text..., however I have read that is is not the recommended method.
A quick way that you could do it if you want to pass this for a range of integers is using assign (base) which creates variables and paste (base),
for (i in 1:10){ #or whatever range you want to use
#assign(name of var, the value of var (here 2i, but whatever function you need to pass))
assign(paste("var", i, sep = "_"), i*2) }
This does not need to a be function as it exports automatically variables to your environment, so if you use it inside the function, you will need to return it to put it to global environment.
In GeoDMS, a geographic coding language by Object Vision, I cannot run code in GeoDmsRun.exe, which I could run without problems in GeoDmsGui.exe. The problem is that it cannot find the parameter 'Values' which is indeed not defined, but apparently implicit somewhere in GeoDMS. The GUI could find this parameter.
I tried defining the Values that lookup is looking for explicitly using
attribute<uint32>values1:=values;
But that didn't work. It would be best to get this lookup functionality without having to use any implicit variables, but how to do that?
Code:
unit<uint32> heatNet2 := unique(buildingWithHeatDemand/roadID)
, dialogType = 'map'
, dialogData = 'geometry'
{
attribute<rdc> geometry(arc) := lookup(values,input/geographic/roads/geometry);
}
Version: 7177
Thanks for helping!
The unique(D->V) operator indeed defines an attribute E->V with the name values of the resulting unit E that maps that resulting unit E to the found values of V. GeoDmsRun.exe should process scripts the same way as GeoDmsGui.exe does, so it is a good idea to report this as issue at http://www.mantis.objectvision.nl.
Meanwhile you can try to define the values attribute explicitly:
unit<uint32> heatNet2 := unique(buildingWithHeatDemand/roadID)
, dialogType = 'map'
, dialogData = 'geometry'
{
attribute<input/geographic/roads> values(heatNet2);
attribute<rdc> geometry(arc) := lookup(values,input/geographic/roads/geometry);
}
The now explicitly defined values will refer to the attribute of the result of the unique operator.
I have an R function with optional parameters like so:
myFunc <- function(
requiredParam,
optionalParam1 = optionalValue1,
optionalParam2 = optionalValue2,
...
optionalParamN = optionalValueN) {
# implementation
}
I have another function which calls this function and has the necessary parameters stored in a dataframe:
optionalParam1 optionalParam3 optionalParam10
1 "val1" "val2" "val3"
I only want to pass the optional parameters specified in the dataframe. For the others, I want it to use the default values. How can I accomplish this without typing up all permutations of optionalParameters existing/not existing?
Call the function using do.call (not knowing what your data.frame is called I will just assume you have a list or something of the parameters called myParams):
do.call(myFunc, as.list(myParams))
You can also build your function call as a string by parsing your dataframe column names and using paste.
Then, use eval(parse(text="your string"))
I am trying to create an empty map, that will be then populated within a for loop. Not sure how to proceed in Rascal. For testing purpose, I tried:
rascal>map[int, list[int]] x;
ok
Though, when I try to populate "x" using:
rascal>x += (1, [1,2,3])
>>>>>>>;
>>>>>>>;
^ Parse error here
I got a parse error.
To start, it would be best to assign it an initial value. You don't have to do this at the console, but this is required if you declare the variable inside a script. Also, if you are going to use +=, it has to already have an assigned value.
rascal>map[int,list[int]] x = ( );
map[int, list[int]]: ()
Then, when you are adding items into the map, the key and the value are separated by a :, not by a ,, so you want something like this instead:
rascal>x += ( 1 : [1,2,3]);
map[int, list[int]]: (1:[1,2,3])
rascal>x[1];
list[int]: [1,2,3]
An easier way to do this is to use similar notation to the lookup shown just above:
rascal>x[1] = [1,2,3];
map[int, list[int]]: (1:[1,2,3])
Generally, if you are just setting the value for one key, or are assigning keys inside a loop, x[key] = value is better, += is better if you are adding two existing maps together and saving the result into one of them.
I also like this solution sometimes, where you instead of joining maps just update the value of a certain key:
m = ();
for (...whatever...) {
m[key]?[] += [1,2,3];
}
In this code, when the key is not yet present in the map, then it starts with the [] empty list and then concatenates [1,2,3] to it, or if the key is present already, let's say it's already at [1,2,3], then this will create [1,2,3,1,2,3] at the specific key in the map.
I need to write a user-defined function that, when applied to a data frame, will return the column position, the column name, the mode, and the class for each variable. I am able to create one that returns mode and class, but I keep getting errors when I include the position/name. I have been doing this,
myFunction <- function(x) {
data.frame(mode(x), class(x))
}
data.frame(names(myData), myFunction(myData))
and it returns the correct info, but it doesn't combine it into a single function I need. Any advice?
You can combine it as follows:
myFunction <- function(x)
data.frame(mode(x), class(x), cname=names(x), cpos=1:ncol(x))