Dacav
Dacav

Reputation: 14078

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.

Upvotes: 11

Views: 24165

Answers (3)

Foad S. Farimani
Foad S. Farimani

Reputation: 14016

lets go through your points one by one:

  1. "ScicosLab, a (pretty badly designed) clone of Scilab" This in my opinion is an inaccurate way of introducing the software. ScicosLab is not a clone of Scilab but a fork of it. The team behind ScicosLab (INRIA) are the ones who made scocos (now called xcos in Scilab development line). At some point (from Scilab v4) the Scilab team decided to move away from Tcl/tk towards Java, but the SciccosLab/scicos team departed, keep using the language (Tcl) and it's graphical user interface design package (tk). Giving the ScocosLab community the credit that the whole Scilab documentation and support is not very good in general. :) (more about Scilab and the forks here)
  2. Regarding the technical question I'm not sure what you are trying to achieve here, Scilab/ScicosLab still have the eval function which basically does what you want. However this function is to be deprecated in favor of evstr. There is also the execstr function which worth studying.
  3. The loadmatfile, as far as I have understood, "tries" to load the variables defined in a MATLAB .mat file (MATLAB's proprietary tabular format) into the Scilab workspace. For example if there is a variable foo it will "try" to create the variable foo and loads its value from the MATLAB script. Check this example. I would create a variable x(i) = foo in the for loop. again your question is not completely clear.
  4. As a side note maybe you could consider exporting your data as CSV instead of .mat files.

Upvotes: 0

Oli
Oli

Reputation: 16045

@arne.b has a good answer.

In your case you can also do that in matlab:

a=load('filename.mat')
x=a.('variable_name')

Upvotes: 9

arne.b
arne.b

Reputation: 4330

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

Upvotes: 12

Related Questions