Example:
I save data to a MX file, like:
a=Range[100];
b=Range[1000];
Export["data.mx", {a,b}, "MX"];
Can I after importing the data:
data = Import["data.mx", "MX"];
also retrieve the names of the variables a,b?
Example:
I save data to a MX file, like:
a=Range[100];
b=Range[1000];
Export["data.mx", {a,b}, "MX"];
Can I after importing the data:
data = Import["data.mx", "MX"];
also retrieve the names of the variables a,b?
No, because those names were never saved into the file.
If you want the names saved too, you can use DumpSave instead of Export for saving, and Get instead of Import for loading. In this case, a and b will be overwritten upon Get. I do not like this method because I need to remember those names, and because I consider overwriting dangerous.
Personally I would export an association instead of a list:
<|"a" -> a, "b" -> b|>
This way I can make the structure of the data self-explanatory.
Export["data.mx", <|"a" -> a, "b" -> b|>, "MX"];? and reading would be: data = Import["data.mx"]? After reading how do I assign a=... and b=...?
– lio
May 10 '17 at 13:57
a and b after reading, then I suggest you use DumpSave/Get.
– Szabolcs
May 10 '17 at 13:59
setName[name_String, value_] := ToExpression[name, InputForm, Function[s, s = value, HoldAll]] Now setName["a", 1] sets the value of a to 1. So you can do KeyValueMap[setName, <|"a" -> 1, "b" -> 2|>] to set a to 1 and b to 2.
– Szabolcs
May 10 '17 at 19:40