# Substituting Data from File

I have sets of Diff. Eq. that I'd like to solve symbolically, then substitute in numerical values for some of the symbols. I'd like to store the value for each symbol in a file, e.g., an excel spreadsheet, then substitute these values in once I have the required solutions.

I'm playing with a toy example:

I have a two-column excel file with letters in the first column and numbers in the second. I have an expression that I wanted to substitute into: f = a + b + c + d.

I import the data, s = Import["File.xlsx"], then try to substitute into my equation using a loop:

For[ i=1, i<=4, i++, f = f/.s[[1,i]][]->s[[1,i]][] ]


This doesn't seem to work. As far as I can tell, it's the difference between the string in the 1st column of s, ith row, e.g., a, and the variable within f that I want to substitute for.

What is the correct way to do this?

You are correct: there is a distinction between the string "a" and the symbol a, and that is presumably why your approach doesn't work. Whenever pattern matching doesn't work as expected it is a good idea to look at FullForm or at least InputForm to see what causes the problem. Here is the imported data (I prepared an excel file as you described and import the first sheet):

data = Import["file.xlsx"][]


Looking at the InputForm it becomes clear that the result is a list of strings and numbers:

InputForm[data]

(*
==> {{{"a", 1.}, {"b", 2.}, {"c", 3.}, {"d", 4.}}}
*)


On the other hand your definition uses symbols:

f = a + b + c + d

(*
==> a + b + c + d
*)


So what you need to do is to convert either way, here I turn the strings into symbols (and of course I strongly discourage to use a For loop to do so, here I use a shortcut for Apply at 1st level):

f /. (Symbol[#1] -> #2 & @@@ data[])

(*
==> 10.
*)


in fact it might be somewhat safer to actually go the other way, that is define f in terms of strings -- that way there is no danger that global definitions for any of the symbols would cause unexpected results and the whole procedure will even become more simple:

f = "a" + "b" + "c" + "d"

f /. Rule @@@ data