# Question regarding exporting of mx files

I want to export a bunch of data to .mx files. So I created a function

str = (Print["a\n"]; Print[a]; Print["\nb\n"]; b);


where a and b are two lists..Then I did

Export["eg.mx",str]


and then when I tried to import

Import["eg.mx"]


it is giving only b list and not list a. What is wrong with my function?

• Try str = Grid[{{"a =", a}, {"b =", b}}]. Is that what you want? – Oleksandr R. Jan 15 '16 at 19:08

It is a common mistake to assume something has to be printed in order to be there somehow. I cannot express this better, but something like this must be the case.

You are wrong. Printing is a function that prints something on the screen. You cannot print something into a variable. The solution is very simple:

a = {1, 2};
b = {3, 4};

str = {a, b};
Export["eg.mx", str]


and then you can do

{a2, b2} = Import["eg.mx"]


Of course, you don't need str at all. You can simply use Export["eg.mx", {a,b}].

I need an output that looks something like "a ={1,2} ,b={3,4}" .But if we do by this method we are getting a list of two {{1,2},{3,4}}. Although we can parse from that, but is there any way we can just export that in that format i have given.

I'm really confused. First, you don't create any output that looks like anything when you export to mx because file.mx is a binary format. You cannot read it.

I have another idea what you could mean by "just export that in that format i have given". You can use DumpSave to store variables. To be exact, you store symbols and there associated values. Therefore, consider this example:

a = {1, 2};
b = {3, 4};
DumpSave["eg2.mx", {a, b}]


What you did here is really store the variables a and b. Therefore, let's clear them in your current session

ClearAll[a,b]


Now, you can call

Get["eg2.mx"]


and magically the variables a and b are defined again and have their values:

a + b
(* {4, 6} *)

• Hi @halirutan...I need an output that looks something like "a ={1,2} ,b={3,4}" .But if we do by this method we are getting a list of two {{1,2},{3,4}}. Although we can parse from that, but is there any way we can just export that in that format i have given – no-one Jan 15 '16 at 16:49
• I'm not entirely sure I get what you mean, because words like parse and format just don't make sense in this Mathematica context. Nevertheless, I have updated my answer at the end with a different approach. – halirutan Jan 15 '16 at 17:03

When I ran your code using an example list, this is what I got:

a = {1, 2};
b = {3, 4};
str = (Print["a\n"]; Print[a]; Print["\nb\n"]; b);

a

{1, 2}

b


So I'm guessing that this should work (I'm just adding on actually printing b):

str = (Print["a\n"]; Print[a]; Print["\nb\n"]; Print[b]);


This worked for me when I did it. Is this what you meant? This is the output I received:

a

{1, 2}

b

{3, 4}

• Hi @nbingo..thank you for your reply but that is not what i wanted. In fact if i remove the semicolon at the end of statement of str, we can get "a" as well as "b". But I need a way to export the contents in that manner to an mx file and import them back in the same format. – no-one Jan 15 '16 at 16:55