# Exporting numerous files (spectra)

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

Do[
Export[
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 = {};

Dynamic[i]
Do[
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];

(*Show[ListPlot[fitdatnorm\[LeftDoubleBracket]1500-a;;1900-a\\[RightDoubleBracket],PlotRange\[Rule]Full],Plot[fit2[t],{t,1450,1900}\,PlotStyle\[Rule]{Red,Thick},PlotRange\[Rule]Full]]*)

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

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

(*ListPlot[subdat\[LeftDoubleBracket]1500-a;;1900-a\\[RightDoubleBracket]]*)

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

{i, 1, 18}]

ListPlot[intlistpaint, PlotRange -> Full]
intlistpaint
intlist

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

• why the back tick after ToString? Syntax error you repeat in several places. – george2079 Jan 23 '18 at 16:49

I think your error is writing

 fitdatnorm[[n; All]]


for

 fitdatnorm[[n, All]]


which can be further reduced to just

fitdatnorm[[n]]


### Update

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},
Do[
f = Iterpolation[Import[files[[i]]], InterpolationOrder -> 1];
fitdat = Transpose[{Table[x, {x, a, 4000}], Table[f[x], {x, a, 4000}]}];
fitdatnorm =
Transpose[
{Transpose[fitdat][[1]],
(Transpose[fitdat][[2]] - fitdat[[1820 - a,2]])/fitdat[[1738 - a,2]]}];
Export[
StringJoin[NotebookDirectory[], "ZnO_TiO2_normalized", ToString[n], ".csv"],
fitdatnorm],
{i, n}]]

• 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 ? – L. Chassou Jan 23 '18 at 19:14
• @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. – m_goldberg Jan 23 '18 at 22:27
• 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 !! – L. Chassou Jan 29 '18 at 14:43
• @L.Chassou. What do you get when you run my code? What goes wrong? – m_goldberg Jan 29 '18 at 14:48
• 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 ? – L. Chassou Jan 31 '18 at 8:46