I wonder how I could export a list of files from Mathematica to my notebook directory.

I use Mathematica to normalize my spectra (CSV files) and then, I would like to export them back to my notebook directory once they are normalized. I tried some codes, but I don't know where the error is coming from.

My files are infrared spectra, they are basically two colums of data (the wavelength from 450 to 4000 and the absorption value for each wavelength). For now, with the followig code, I am able to export all of them, but the first one is complete, the second file starts at 451, the third one at 452, the fourth one at 453 (and so far and so on).

The plot is the following, and my problem is with the last part

    NotebookDirectory[] <> "ZnO_TiO2_normalized" <> ToString[n]` <> ".csv", 
    fitdatnorm[[n; All]]], 
  {n, 1, 18, 1}];" 

but I give all the code in case of you need it.

min = 1500;
max = 1900;
a = 450;

intlistpaint = {};

 datpaint1 = Import[files[[i]]];
 f[x_] = Interpolation[datpaint1, InterpolationOrder -> 1][x];
 fitdat = {Table[x, {x, 450, 4000}], 
    Table[f[x], {x, 450, 4000}]}\[Transpose];
 fitdatnorm = {fitdat\[Transpose][[1]], (fitdat\[Transpose][[2]] - fitdat[[1820 - a, 2]])/ fitdat[[1738 - a, 2]]}\[Transpose];

 fit2 = NonlinearModelFit[fitdatnorm[[1700 - a ;; 1900 - a]],
   {pearson[x, x0, c, \[CapitalDelta], m, \[Nu]]},{{x0, 1738}, {c, 0.5},{\[CapitalDelta], 35}, {m, 0.65}, {\[Nu], 0.65}}, x];


 fitpoints = Table[fit2[j], {j, fitdatnorm\[Transpose][[1]]}];

 subdat = {fitdatnorm\[Transpose][[1]], 
    fitdatnorm\[Transpose][[2]] - fitpoints}\[Transpose];


 AppendTo[intlistpaint, Integrate[Interpolation[subdat,InterpolationOrder -> 1][x], {x, 1510, 1620}]];,

 {i, 1, 18}]

ListPlot[intlistpaint, PlotRange -> Full]

Do[Export[NotebookDirectory[] <> "ZnO_TiO2_normalized" <> ToString[n]` <> 
".csv", fitdatnorm[[n; All]]], {n, 1, 18, 1}];
  • $\begingroup$ why the back tick after ToString? Syntax error you repeat in several places. $\endgroup$ – george2079 Jan 23 '18 at 16:49

I think your error is writing

 fitdatnorm[[n; All]]


 fitdatnorm[[n, All]]

which can be further reduced to just



I have given your code another look and now I think your problem is that fitdatnorm is only holding the normalized data from a single iteration of the 1st Do. Therefore, when you run the 2nd Do, you only have the normalized data from the last (18th) file. That would explain the results you report in your question.

You can test my hypothesis by running the following code. Since this code does both import and export in one Do-loop, it should write the files correctly.

With[{a = 450, n = 18}, 
    f = Iterpolation[Import[files[[i]]], InterpolationOrder -> 1]; 
    fitdat = Transpose[{Table[x, {x, a, 4000}], Table[f[x], {x, a, 4000}]}]; 
    fitdatnorm = 
         (Transpose[fitdat][[2]] - fitdat[[1820 - a,2]])/fitdat[[1738 - a,2]]}]; 
      StringJoin[NotebookDirectory[], "ZnO_TiO2_normalized", ToString[n], ".csv"], 
    {i, n}]]
  • $\begingroup$ Hi ! Thank you for the answer. I tried both of them before adding the post, but they are not working : I export the correct number of files, but the first file only contains the first value (450 and its absorption), the second one contains the second value (451 and its absorption), the third one only contains the third value (452 and its absorption) (...) Do you have any clue ? $\endgroup$ – L. Chassou Jan 23 '18 at 19:14
  • $\begingroup$ @L.Chassou. I have updated my answer with some code for you to try. Report back the results and I will give further help if your findings indicate it is needed. $\endgroup$ – m_goldberg Jan 23 '18 at 22:27
  • $\begingroup$ Hi ! I tried with the new code you gave me, but I don't get it yet. You may be right, there is probably someting wrong with "fitdatnorm". I tried with your code, but I am not sure where I should place it ? Directly at the beginning or later in the code ? Thanks !! $\endgroup$ – L. Chassou Jan 29 '18 at 14:43
  • $\begingroup$ @L.Chassou. What do you get when you run my code? What goes wrong? $\endgroup$ – m_goldberg Jan 29 '18 at 14:48
  • $\begingroup$ When I run the code separetely, it takes a really long tie (around hours) to export the files. When I tried to add your code to my first part, it does not work. This is why I was asking you where I should include your part of the code, in an independant section after my code, or in my code itself ? $\endgroup$ – L. Chassou Jan 31 '18 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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