Consider that I have the simple graph of

Graph[{"Uppsala" -> "Marsta", "Marsta" -> "Uppsala", "UpplandsVasby" -> "Sollentuna", "Sollentuna" -> "UpplandsVasby", 
  "UpplandsVasby" -> "Marsta", "Marsta" -> "UpplandsVasby", 
  "Stockholm" -> "Boo", "Boo" -> "Stockholm", 
  "Stockholm" -> "Lidingo", "Lidingo" -> "Stockholm", 
  "Stockholm" -> "Sollentuna", "Sollentuna" -> "Stockholm", 
  "Stockholm" -> "Taby", "Taby" -> "Stockholm"}, 
 VertexLabels -> "Name"]

I want to add required data of the Graph such as EdgeWeight, EdgeCapacity or other required data with GUI. Preferably in Excel interactive data sheet . Could you please somebody help me on that?

  • $\begingroup$ If you want to do it with Excel, just do that. When you're done, show us the table you created and ask about how to convert that to a Mathematica graph. $\endgroup$ – Szabolcs May 11 '13 at 14:42
  • $\begingroup$ What have you tried? Sounds to me like you may want a matrix in your spreadsheet, possibly one for each quantity you want to specify: edge weight, capacity, ... with zero edge weight specifying no-connection. When you have it import it and build your tree from the matrix there with AdjecencyGraph. $\endgroup$ – SEngstrom May 11 '13 at 14:42
  • $\begingroup$ No I dont want to do that in Excel.It is already in Mathematica .You can see the sample code.But my question is how I can interact with Mathematica in giving my mentioned data of graph. $\endgroup$ – Alex May 11 '13 at 15:02
  • $\begingroup$ What is an "Excel interactive data sheet" in a Mathematica context? Your question really needs some clarification about what you really want, otherwise you leave us guessing. $\endgroup$ – Sjoerd C. de Vries May 11 '13 at 15:09
  • 1
    $\begingroup$ @Alex I'm not sure I understand what you are asking. In your previous comment you said, "No I dont want to do that in Excel.", then "I want to fill in the required data in Excel with automatically transferable to GUI and Mathematica." Can you clarify a bit? $\endgroup$ – Szabolcs May 11 '13 at 20:27

I'm not quite sure what exactly you're looking for. However here is a little GUI I wrote that shows the kinds of things you can do:

DynamicModule[{g, toCSV, fromCSV, fromExcel, file, properties},
 properties = {EdgeWeight, EdgeLabels};

 toCSV[g_Graph] := Module[{edges, props, rows},
   edges = List @@@ EdgeList[g];
   props = Table[PropertyValue[{g, e}, p], {e, edges}, {p, properties}];
   rows = Flatten /@ Thread[{edges, props}];
   rows = Join[{Join[{"From", "To"}, properties]}, rows];
   Map[ToString, rows, {2}]];

 fromCSV[csv_] := Module[{beheaded = Rest[csv]},
   g = Graph[Property[#1 -> #2, Thread[properties -> {##3}]] & @@@ beheaded]];

      FileNameSetter[Dynamic[file], "Save", Appearance -> Button[Dynamic[file]]],
      Button["open", SystemOpen[file]]}, SpanFromLeft},

   {InputField[Dynamic[g], Expression, ImageSize -> All],
      Button["export", Export[file, toCSV[g]]],
      Button["import", g = fromCSV[Import[file]];
       Paste[g = SetProperty[g, ImageSize -> {200, 200}]]]}}

You can use it like this:

enter image description here

or copy-paste a graph object like this:

enter image description here

Then you click the export button, push the open button, and the file will open in your Excel, where you can edit it:

enter image description here

Then you Save it in your Excel, and click the import button in Mathematica. That will automatically update the graph object in the InputField:

enter image description here

It will also Paste the imported graph object wherever you have the cursor at. (Depending on your usage you might want to reverse the order of the export/import buttons). As I said, this is to show the kinds of things you can do. I don't know if all the properties you need can be accessed this way and there may be issues with file-handling corner cases.


For a more complete solution I would probably use a scheme like this:

conversionTable = {
  {"EdgeWeight", PropertyValue[{g, #}, EdgeWeight] &, SetProperty[{g, #1}, EdgeWeight -> #2] &},
  {"EdgeLabel", PropertyValue[{g, #}, EdgeLabels] &, SetProperty[{g, #2}, EdgeLabels -> #2] &},
  {"Directed", DirectedEdge @@ # === # &, VertexReplace[g, {#1 -> If[#2, DirectedEdge, UndirectedEdge] @@ #1}] &},
  . . .

({property name, property-getter function, property-setter function}). This way you could export/import arbitrary properties.


I forgot to mention this, but it is important. I think a better solution is to forget Excel altogether and just use Mathematica. Example:

DynamicModule[{m = RandomInteger[{0, 1}, {2, 2}]},
   {InputField[Dynamic[MatrixForm[m], (m = #1) &], Expression, FieldSize -> Full],

enter image description here

(Using an adjacency matrix setup here for simplicity). See also my post here for an example of how you could construct an array of inputs.

| improve this answer | |
  • $\begingroup$ Many thanks!!!That very nice advice of you but the reason the I need to use Excel is we suppose to interact with other software.I already developed an optimizer in Mathematica and the other software suppose to interact with this optimizer .That why I asked because that GUI is necessary to see and perhaps modify the data manually.Thanks for great help cause I am not software developer using your professional expertise.I have to admit that these you provided push me to think more over. $\endgroup$ – Alex May 12 '13 at 18:40
  • $\begingroup$ I have to then rephrase my question .Can I have the possibility to click on the Edges and Vertexes,on the graph itself and be able to change the data on the graph?Your provided last solution is just for AdjacencyMatrix and I want to modifyEdgeWeight,VetrexWeight and so on. $\endgroup$ – Alex May 13 '13 at 11:30
  • $\begingroup$ @Alex I was thinking about something along those lines. I'll play around with things but I don't know if I will have enough time to actually write anything complete. It might take me a week or two. :) $\endgroup$ – amr May 13 '13 at 18:51
  • $\begingroup$ great!Many thanks for the help! $\endgroup$ – Alex May 13 '13 at 18:55

If you own a license of ExcelLink, you can proceed like this:

(*open a blank Excel sheet now*)

s = {"Stockholm" -> "Boo", "Boo" -> "Stockholm", 
     "Stockholm" -> "Lidingo", "Lidingo" -> "Stockholm", 
     "Stockholm" -> "Sollentuna", "Sollentuna" -> "Stockholm", 
     "Stockholm" -> "Taby", "Taby" -> "Stockholm"};

(* fill in the excel sheet *)
Excel["A:B"] = List @@@ s; 
(*go on and modify the names in excel*)

Perform your modifications in the Excel sheet, and then:

Graph[Rule @@@ Excel["A:B"]]
| improve this answer | |
  • $\begingroup$ I have to then rephrase my question .Can I have the possibility to click on the Edges and Vertexes,on the graph itself and be able to change the data on the graph?Your provided last solution is just for AdjacencyMatrix and I want to modifyEdgeWeight,VetrexWeight and so on. $\endgroup$ – Alex May 13 '13 at 11:33

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.