2
$\begingroup$

I have a long Mathematica code in a notebook, denoted by NB1. Since it is a very long code, and I need only some of the outputs from NB1 to be used in another notebook denoted by NB2, I assigned Cell Tags to those NB1 cells generating the outputs required by NB2. I like to know how to recall the tagged cells in NB1 into NB2.

I know how to run NB1 while in NB2 but I do not want to run all the operations in NB1 as I only need a subset of the NB1 outputs.

Here is an example of NB1:

Clear[Evaluate[Context[] <> "*"]];
Clear[n, d, G, transfer, absorption, info, capParam, infoStock, 
edgeCapacityMat, infoStocks, system, reducedSystem, sa, wG];

SeedRandom[14];
n = 6;
d = 0.3;
G = RandomGraph[{Round[n], Round[n*(n - 1)*d]}, DirectedEdges->
True];

transfer = Table[t[i], {i, 1, n}]; (* transfer capacity *)
absorption = Table[a[i], {i, 1, n}]; (* absorption capacity *)
info = Table[x[i], {i, 1, n}]; (* information stock capacity *)

(* parametrization *)
capParam =Table[{t[i] -> RandomReal[], a[i]->RandomReal[]}, 
{i,  1,n}]//Flatten;
infoStock = 
Table[x[i] -> RandomInteger[{1, 10}], {i, 1, n}]//Flatten;

edgeCapacityMat[transferCap_, 
absorptionCap_] := (# - DiagonalMatrix[Diagonal[#]]) &[
KroneckerProduct[transferCap, absorptionCap]];
infoStocks[stock_] := DiagonalMatrix[stock];
system = infoStocks[info].edgeCapacityMat[transfer, 
absorption];  (* fully defined system *)

reducedSystem = 
AdjacencyMatrix[G]*
system;  (* the system associated with AdjacencyGraph "G" *)
sa = SparseArray[reducedSystem];
wG = Graph[sa["NonzeroPositions"], 
EdgeWeight->sa["NonzeroValues"],DirectedEdges 
->True,VertexLabels -> "Name"];

(* @KGLR's answer: use this in formulating the "attention   
allocation problem in my paper  *)

ClearAll[edgeW];
edgeW = Module[{g = #, 
e = DirectedEdge @@@ Partition[#, 2, 1] & /@ 
   FindPath[##, \[Infinity], All]}, 
Transpose[{e, PropertyValue[{g, #}, EdgeWeight] & /@ # & /@
e}]]&;

desiredOutput1=edgeW[wG, 5, 2]
desiredOutput2=HighlightGraph[wG, edgeW[wG, 5, 2][[All, 1]]]

I like to use desiredOutput1 and desiredOutput2 in NB2, therefore I created two cell tags in NB1 to be recalled from NB2, but I do not know how to recall only the two cell tags from NB2.

Thank you for your help.

$\endgroup$
2
$\begingroup$

You can use NotebookEvaluate with the EvaluationElements option. Here is an example:

ClearAll[x, y]; (* x and y are unassigned *)
nb = CreateDocument @ {
  ExpressionCell[
    Defer[
      x = 1;
      CreateDialog[{"X", DefaultButton[]}]],
      "Input", 
      CellTags -> {"a"}], (* we assign this cell tag 'a' *)
  ExpressionCell[
    Defer[
      y = 2;
      CreateDialog[{"Y", DefaultButton[]}]],
      "Input", 
      CellTags -> {"b"}] (* we assign this cell tag 'b' *)
};
(* now, run only tag 'a' *)
NotebookEvaluate[nb, EvaluationElements -> {"Tags" -> {"a"}}]
{x, y}

{1, y}

$\endgroup$
  • $\begingroup$ Your answer works fine, however, I have already assigned Tags to each cell in myCode by using Cell-Add/Remove option, and I simply want to call those Tags into NB2. If I assign names for each calculation in NB1, I can simply retrieve them to NB2, no problem with that. But I do not want that. I like to retrieve an individual cell as a stand-alone element into NB2. I hope there is much less complicated version of your Code to bring all the cell tags into NB2. $\endgroup$ – Tugrul Temel Jul 13 at 5:58

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.