2
$\begingroup$

I have an excel file i would like to import as data in mathematica with 3 columns like below:

Column 1: the nodal numbers Column 2: the values of mx Column 3: the value of mxy

Once ive imported the data, i would like to save it as a variable, such that mx[13] would equal to the value of mx for the nodal 13 or mxy[2] would equal to the value of mxy for nodal 2. I cannot save it straight away as some nodal numbers are skipped.

Thank you

$\endgroup$
3
  • $\begingroup$ Try mx = Import[filename]. $\endgroup$
    – bill s
    Commented Feb 18, 2017 at 2:31
  • $\begingroup$ I cant do that becauae some nodal numbers are missing. Importing would just name it as mx[1], mx[2], mx[3] and so forth. But if number 2 is missing, i'd have to skip mx[2] $\endgroup$ Commented Feb 18, 2017 at 2:45
  • 2
    $\begingroup$ Well then, you'll have to provide a sample of your file, since you haven't provided enough information to solve your problem. $\endgroup$
    – bill s
    Commented Feb 18, 2017 at 2:57

2 Answers 2

3
$\begingroup$

The first thing I do after I Importa spreadsheet is check its dimensions, like this

xlsx = Import["/path/to/filename"];
Dimensions[xlsx]

(*  {1,4,3}  *)

This tells me there is one spreadsheet, and it has 4 rows and 3 columns.
To see the data, I would evaluate

nSheet = 1;
Dataset[xlsx[[nSheet]]]

That would give me a nice view of the data on the selected sheet. When I look at the data I notice things like whether the node numbers are Real or maybe some of my values are text.

There are 2 ways to get the row for node 13, say. One way is

node = 13;
First[Select[xlsx[[nSheet]], First[#] == node &]]

This returns only the first row that has a value in first column that is numerically equal to the node number. This form works when we give an integer node number, but the data contains real values for the node numbers. If the node numbers were text Strings, this would not work. We can use this to define a function, mx that picks the second column of the row, as

Clear[mx]
mx[n_] := First[Select[xlsx[[nSheet]], First[#] == n &]][[2]]

The other way to select an entire row with the given node number is

FirstCase[xlsx[[nSheet]], {1.0 * node, ___}]

Now that may look strange, multiplying the node number by one, but we do that to convert from integer to real, because this method is based on matching patterns, not on numerical equality. We could make function to pick the third column of the row like this

Clear[mxy]
mxy[n_] := FirstCase[xlsx[[nSheet]], {1.0 n, ___}][[3]]

Which way is better? Personally, I would use the first method, based on Select for both mx and mxy. If the node numbers in the spreadsheet were of type String, then I would use something similar to the second method, maybe.

There are some other ways to do the same thing. For example, we could create a SparseArray for mx and another SparseArray for mxy. Or, we could create two Associations or we could create Datasets with named rows. I like the Select method because it is easy to understand and easy to code. As long as the Select method works on the data in the spreadsheet, try to use it.

$\endgroup$
1
  • $\begingroup$ thank you! this is what i was looking for.. I used the select option $\endgroup$ Commented Feb 18, 2017 at 8:07
0
$\begingroup$

Use Import to import your raw data from file, then use something like the following function to assign to the indexed variables m[...]. Below I use some made up data as your raw data; in your case that would be your actual data coming from Import:

raw = {{1, 3, 4}, {2, 5, 6}, {4, 7, 8}};
(mx[#1] = #2)& @@@ raw;
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.