Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing variable by string name

I need to load experimental data into scicoslab, a (pretty badly designed) clone fork of scilab which happens to support graphical modeling. The documentation on the web is pretty poor, but it's reasonably similar to scilab and octave.

The data I need to process is contained into a certain number of text files: Data_005, Data_010, …, Data_100. Each of them can be loaded using the -ascii flag for the loadmatfile command.

The problem comes from the fact that loadmatfile("foo", "-ascii") loads the file foo.mat into a variable named foo. In order to to cycle on the data files, I would need to do something like:

for i = [5:5:100]
    name = sprintf("Data_%02d", i);
    loadmatfile(name, "-ascii");
    x = read_var_from_name(name);
    do_something(x);
end

where what I search for is a builtin read_var_from_name which would allow me to access the internal symbol table by string.

Do you know if there exist a similar function?

Notes:

  1. There's no way of overriding this behavior if your file is in ascii format;
  2. In this phase I could also use octave (no graphical modelling is involved), although it behaves in the same way.
like image 593
Dacav Avatar asked Apr 02 '12 11:04

Dacav


People also ask

How do you use a variable in a string in Python?

If you can depend on having Python >= version 3.6, then you have another attractive option, which is to use the new formatted string literal (f-string) syntax to insert variable values. An f at the beginning of the string tells Python to allow any currently valid variable names as variable names within the string.

How do you get the value of a variable in Python?

Python variables store values in a program. You can refer to the name of a variable to access its value. The value of a variable can be changed throughout your program. Variables are declared using this syntax: name = value.


2 Answers

>> foo = 3.14; name = 'foo'; eval(name)

foo =

    3.1400

The above works in MATLAB, and Scilab's documentation says it also has an eval function. Not sure if I understood you correctly, though.

like image 72
arne.b Avatar answered Sep 28 '22 23:09

arne.b


@arne.b has a good answer.

In your case you can also do that in matlab:

a=load('filename.mat')
x=a.('variable_name')
like image 44
Oli Avatar answered Sep 29 '22 00:09

Oli