The first thing I do after I Import
a 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.
Import[filename]
. $\endgroup$