1
$\begingroup$

I have a multivariable function, I want to produce the data of that function, export the data and use the data for later purposes. However, the imported is not recognized as data. Here is an example:

f[x_,y_,z_]=x*y*z;
data=Table[{{x,y,z},f[x,y,z]},{x,1,2,1},{y,1,2,1},{z,1,2,1}];

The output is:

{{{{{1, 1, 1}, 1}, {{1, 1, 2}, 2}}, {{{1, 2, 1}, 2}, {{1, 2, 2}, 4}}}, {{{{2, 1, 1}, 2}, {{2, 1, 2}, 4}}, {{{2, 2, 1}, 4}, {{2, 2, 2}, 8}}}}

The format I need is {{x,y,z},f[x,y,z]}. So for this, I Flatten the data:

data//Flatten[#,2]&

here is the output:

{{{1, 1, 1}, 1}, {{1, 1, 2}, 2}, {{1, 2, 1}, 2}, {{1, 2, 2}, 4}, {{2, 1, 1}, 2}, {{2, 1, 2}, 4}, {{2, 2, 1}, 4}, {{2, 2, 2}, 8}}

Until now everything is perfectly working, and I can interpolate this data. I want to save this data for later use and interpolate it in my future works. Therefore, I export it:

Export["mydata.csv", data//Flatten[#,2]&] 

In the next step I import the exported data and the problem appears here. Here is the imported the same data:

{{{1, 1, 1}, 1}, {{1, 1, 2}, 2}, {{1, 2, 1}, 2}, {{1, 2, 2}, 4}, {{2, 1, 1}, 2}, {{2, 1, 2}, 4}, {{2, 2, 1}, 4}, {{2, 2, 2}, 8}}

As you see, it is exactly similar to the exported data.

I try to interpolate the imported "mydata.csv". However, it gives this error:

Interpolation[Import["mydata.csv"], InterpolationOrder-> 1]
Interpolation::indat: Data point {{1, 1, 1},1} contains abscissa {1, 1, 1}, which is not a real number

I know that everything is related to the way I am exporting. I tried to define a format for exported data, such as "Table" and "Data", but it did not work. My main purpose is to interpolate data and I need it to be in the format of {{{x1,y1,z1},f[x1,y1,z1]},{{x2,y2,z2},f[x2,y2,z2]},... }.

I will be very appreciative if someone helps me.

$\endgroup$
4
  • $\begingroup$ data = data // Flatten[#, 2] &; Export["mydata.csv", data]; Import["mydata.csv"] $\endgroup$
    – cvgmt
    Commented Aug 20, 2022 at 1:23
  • $\begingroup$ Thank you so much for your reply. So, already did it. Now I want to interpolate the imported "mydata". It gives me this error: Interpolation::indat: Data point {{1, 1, 1},1} contains abscissa {1, 1, 1}, which is not a real number $\endgroup$
    – Mehrdad
    Commented Aug 20, 2022 at 18:17
  • $\begingroup$ What is the version of your Mathematica? In ver 13.0.1, f=Interpolation[data, InterpolationOrder -> 1] correctly produces the interpolating function: {(f @@ #[[1]]), #[[2]]} & /@ data gives {{1,1},{2,2},{2,2},{4,4},{2,2},{4,4},{4,4},{8,8}}. $\endgroup$
    – A. Kato
    Commented Aug 22, 2022 at 3:31
  • $\begingroup$ I think there is a better way, but try Interpolation[ToExpression@Import["mydata.csv"], InterpolationOrder-> 1] and compare Head/@ToExpression@Import["mydata.csv"][[1]] with Head/@Import["mydata.csv"][[1]] (giving, respectively, {List, Integer} and {String, Integer}) $\endgroup$
    – user1066
    Commented Jan 17, 2023 at 21:33

2 Answers 2

2
$\begingroup$

This is how actually the data looks like after the export/import sequence, therefore the error message when trying to use the Interpolation function:

data = {{{{{1, 1, 1}, 1}, {{1, 1, 2}, 2}}, {{{1, 2, 1}, 2}, {{1, 2, 2}, 
4}}}, {{{{2, 1, 1}, 2}, {{2, 1, 2}, 4}}, {{{2, 2, 1}, 
4}, {{2, 2, 2}, 8}}}};
Export["data.csv", data // Flatten[#, 2] &];
imported = Import["data.csv"];
imported  // InputForm   

{{"{1, 1, 1}", 1}, {"{1, 1, 2}", 2}, {"{1, 2, 1}", 2}, {"{1, 2, 2}", 4}, {"{2, 1, 1}", 2}, {"{2, 1, 2}", 4}, {"{2, 2, 1}", 4}, {"{2, 2, 2}", 8}}

You can simply use ToExpression in order to get a list of integers:

ToExpression /@ imported // InputForm

{{{1, 1, 1}, 1}, {{1, 1, 2}, 2}, {{1, 2, 1}, 2}, {{1, 2, 2}, 4}, {{2, 1, 1}, 2}, {{2, 1, 2}, 4}, {{2, 2, 1}, 4}, {{2, 2, 2}, 8}}

ToExpression /@ imported // Interpolation[#, InterpolationOrder -> 1] &
$\endgroup$
0
$\begingroup$

Try Flatten[...,n] command.

f[x_, y_, z_] = x*y*z;
data = Flatten[
   Table[{{x, y, z}, f[x, y, z]}, {x, 1, 2, 1}, {y, 1, 2, 1}, {z, 1, 
     2, 1}], 2];

will produce

{{{1, 1, 1}, 1}, {{1, 1, 2}, 2}, {{1, 2, 1}, 2}, {{1, 2, 2}, 4}, {{2, 1, 1}, 2}, {{2, 1, 2}, 4}, {{2, 2, 1}, 4}, {{2, 2, 2}, 8}}

I guess this format is what you want.

$\endgroup$
3
  • $\begingroup$ Thank you for your comment. But, my question is not this. The question is that after exporting this data and importing back, I can not Flatten the data or, I can not interpolate the data. $\endgroup$
    – Mehrdad
    Commented Aug 20, 2022 at 1:06
  • $\begingroup$ You need to clarify in more specific way what you want, what you have tried and how it failed.... $\endgroup$
    – A. Kato
    Commented Aug 20, 2022 at 2:25
  • $\begingroup$ Thank you so much for your reply. I edited my question. So, I do what you are doing. Then I export "mydata". Then I import it. In the later step, I want to interpolate "mydata". However, Mathematica gives this error: Interpolation::indat: Data point {{1, 1, 1},1} contains abscissa {1, 1, 1}, which is not a real number. $\endgroup$
    – Mehrdad
    Commented Aug 20, 2022 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.