I have defined a function that returns different output according to the values of the axes of an ellipsoidal. The function is given below:

S[α1_, α2_, α3_, v_] := 
  sortedargs = SortBy[-# &][{α1, α2, α3}];
  Print["Ellipsoidal axes ", " a1: ", α1, ", a2: ", α2, 
   ", a3: ", α3]; 
  Which[(*sphere*)α1 == α2 == α3, 
   Print["spherical inclusion"]; 
   SMat = SSphere[α1, α2, α3, v]; 
   Return[SMat],(*prolate spheroid*)(sortedargs[[2]] == 
      sortedargs[[3]] && α3/α1 <= 1000), 
   Print["Prolate Spheroidal Inclusion"]; 
   SMat = SProlateSpheroid[α1, α2, α3, v]; 
   Return[SMat],(*oblate spheroid*)sortedargs[[1]] == sortedargs[[2]],
    Print["Oblate Spheroidal Inclusion"]; 
   SMat = SOblateSpheroid[α1, α2, α3, v]; 
   Return[SMat], α3/α1 > 1000, 
   Print["cylindrical inclusion"]; 
   SMat = SCylinder[α1, α2, α3, v]; 
   Return[SMat],(*generic ellipsoid spheroid*)True, 
   Print["generic ellipsoid"]; 
   SMat = SEllipsoidGen[α1, α2, α3, v]; 

What I want is if none of the Which statement cases is not satisfied then the function will evaluate the contents of a whole section within the same notebook (that actually contains several numerical integrations) and return as SMat the final output of this section.

How is it possible to do this?

Thank you very much!

PS. For the sake of completeness I provide the link with the original discussion about this function.


  • 1
    $\begingroup$ You can put that whole section into a Module, assign a name to it, like runSection := Module[{}, ...], and then add a last case to Which in the form of True, runSection. $\endgroup$ Sep 8 '15 at 12:47

To evaluate the contents of a whole section within the same notebook and return its output please use the following simple function (which should be placed within the section you want to be evaluated)

EvalSection := Block[{notebook, nb, thissection, result}, 
 "find me 31415"; notebook = NotebookGet[EvaluationNotebook[]]; 
  thissection = Select[Cases[notebook, CellGroupData[a__], Infinity], 
       ! FreeQ[#1, "\"find me 31415\""] & ]; 
  nb = CreateDocument[]; 
  NotebookWrite[nb, thissection]; 
  result = NotebookEvaluate[nb,InsertResults -> True]; 
  NotebookClose[nb]; result]

To test it please run EvalSection outside the section to avoid obvious infinite loops. Enjoy!


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.