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
?
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityExample:
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=...
?
$\endgroup$
a
and b
after reading, then I suggest you use DumpSave
/Get
.
$\endgroup$
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
.
$\endgroup$