2
$\begingroup$

I have this code, which works properly: (I know you do not have the excess to my file mydata.xlsx, but that is not a problem, I believe.

data = Import[
   "mydata.xlsx", {"Data", 1,
     Range[73, 100], {7, 8}}];
xmin = Min@data[[All, 1]];
xmax = Max@data[[All, 1]];
ymin = Min@data[[All, 2]];
ymax = Max@data[[All, 2]];
datanew = 
  Table[{(data[[i, 1]] - xmin)/(xmax - xmin), (data[[i, 2]] - 
       ymin)/(ymax - ymin)}, {i, 1, Length[data]}];
modelf = a + b*Exp[c*(x^d + e)^f];
modelkf[k_] := 
  a + b*Exp[c*(datanew[[k, 1]]^d + e)^f] - datanew[[k, 2]];
res = Sum[Abs[modelkf[k]]^4, {k, 2, Length[datanew]}];
fit = NMinimize[res Length[datanew], {a, b, c, d, e, f}, 
   Method -> "DifferentialEvolution"];
modelf0 = modelf /. fit[[2]];
plot1 = ListPlot[datanew, PlotStyle -> Red];
plot2 = Plot[modelf0, {x, 0, 1}, PlotStyle -> {Thin, Blue}, 
   PlotRange -> All];
data
Show[plot1, plot2];
Show[ListPlot[data, PlotStyle -> Red]]
datanewfit = 
 Table[{(data[[i, 1]]), 
    Round[(a + 
         b*Exp[c ((((data[[i, 1]]) - xmin + 0.001)/(xmax - xmin))^d + 
               e)^f])*(ymax - ymin) + ymin]}, {i, 1, 
    Length[data]}] /. fit[[2]]
Show[ListPlot[datanewfit, PlotStyle -> Black]]

Now, I need to export datanewfit to mydata.xlsx, to Sheet3, and start (for example, in this case) from cell J9.

That is the range J9:K36 will be filled with datanewfit I do not know how should I do that.


Any help would be appreciated. THANKS!

$\endgroup$
  • $\begingroup$ @Bill Sorry dear, I tried but could not find the way. Can you please help me? $\endgroup$ – Hussain-Alqatari Jul 5 '20 at 14:13
2
$\begingroup$
Clear["Global`*"]

sheets = Import["/Users/roberthanlon/Downloads/mydata.xlsx"];

Generate data

SeedRandom[1234]

datanewfit = RandomReal[{0, 100}, {28, 2}];

Replace old data

sheets = ReplacePart[sheets, 
   Thread[Flatten[Array[{3, 8 + #1, 9 + #2} &, Dimensions@datanewfit], 1] -> 
     Flatten[datanewfit, 1]]];

Until you know it is working properly, export to a new workbook rather than the original.

Export["/Users/roberthanlon/Downloads/mydata2.xlsx", sheets];
$\endgroup$

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.