I want to import scientific data from a CSV file. The data are length measurements in centimeters with uncertainties. I want to import the data with uncertainties and units into a vector - please see the code below. I can do this without units, but I am stuck how to do this with units for the whole data set. Any help is appreciated! Thanks!

data=Import["data.csv","CSV", "HeaderLines"->2];
TableForm[{Transpose[data]}, TableHeadings->{None,{"x (cm)","dx(cm)"}}]

enter image description here


enter image description here

I want to add units (cm) to variable x. I want it to be VectorAround[{49.9 cm, 60.2 cm, 70.1 cm, 79.8 cm , 90. cm, 100.2 cm }, {0.1 cm, 0.1 cm, 0.1 cm, 0.1 cm, 0.2 cm, 0.2 cm}].

For example, I can do this manually for two data points:


enter image description here

Is there a better way to do this? This would be highly impractical to do manually with a large data set.

  • 1
    $\begingroup$ Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, $\endgroup$
    – Dunlop
    Feb 15 at 5:44
  • $\begingroup$ 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. $\endgroup$
    – Dunlop
    Feb 15 at 5:44
  • 1
    $\begingroup$ Try applying the function Quantity[] to the entire list of data. I think it has listable attributes meaning it should operate on all members of your data list $\endgroup$
    – Dunlop
    Feb 15 at 5:45
  • 1
    $\begingroup$ Welcome to the Mathematica Stack Exchange. Please load data with a few more entries, so forum participants can experiment with it. Or you can include a 1-click link to a downloadable csv file. Use an online service such as Dropbox or Google drive and make sure the link is public. Thanks. $\endgroup$
    – Syed
    Feb 15 at 8:00
  • 1
    $\begingroup$ @user90441 sure, but you are duplicating code. You don't need to do Quantity twice. $\endgroup$
    – rhermans
    Feb 15 at 16:26

2 Answers 2

data = Table[
  {k + RandomReal[{-1,1}*k/500], Round[k/500, 0.1]}
  , {k, 50, 100, 10}

VectorAround @@ Quantity[
  , "Centimeters"

(* VectorAround[
  Quantity[49.9561, "Centimeters"], 
  Quantity[59.9273, "Centimeters"], Quantity[69.8579, "Centimeters"], 
  Quantity[79.977, "Centimeters"], Quantity[89.8892, "Centimeters"], 
  Quantity[99.9217, "Centimeters"]
  Quantity[0.1, "Centimeters"], 
  Quantity[0.1, "Centimeters"], Quantity[0.1, "Centimeters"], 
  Quantity[0.2, "Centimeters"], Quantity[0.2, "Centimeters"], 
  Quantity[0.2, "Centimeters"]

Or, if you don't want a vector,

 Around @@@ data
 , "Centimeters"

(* {Quantity[(
Around[49.956130623394806`, 0.1]), "Centimeters"], Quantity[(
Around[59.92730212004165, 0.1]), "Centimeters"], Quantity[(
Around[69.85785754443056, 0.1]), "Centimeters"], Quantity[(
Around[79.97703513294425, 0.2]), "Centimeters"], Quantity[(
Around[89.88924493447557, 0.2]), "Centimeters"], Quantity[(
Around[99.9217340877855, 0.2]), "Centimeters"]} *)

Actually this works:

 Quantity[data[[All, 1]], "Centimeters"],
 Quantity[data[[All, 2]], "Centimeters"]]

Thanks for the help everyone!!!


Your Answer

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

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