I'm trying to use a list of data points from a csv file to perform multiple calculations and plot the end result. Basically I have 3 columns of data in my csv, I need the second and third to be pulled in as N1 and N2, and then the first column (wavelength) needs to be the list of x values I am plotting against. I'm completely lost with how to make this loop work.

lines = 100; \[Theta]t = Table[i, {i, lines}]; wavelength = 
 Table[i, {i, lines}];
N1 = Table[i, {i, lines}]; N2 = Table[i, {i, lines}]; k1 = 
 Table[i, {i, lines}]; k2 = Table[i, {i, lines}];
rs = Table[i, {i, lines}]; rp = Table[i, {i, lines}]; Rs = 
 Table[i, {i, lines}]; Rp = Table[i, {i, lines}]; n1 = 
 Table[i, {i, lines}]; n2 = Table[i, {i, lines}];
degrees = 45;
\[Theta]i = degrees*Pi/180;
Ravg0 = Table[i, {{i, lines} {j, 2}}];
z = 1;
While[z < (lines),
  wavelength[[z]] = s[[z, 1]];
  N2[[z]] = s[[z, 2]];
  k2[[z]] = s[[z, 3]];
  n1[[z]] = 1 + k1[[z]]*I;
  n2[[z]] = N2[[z]] + k2[[z]]*I;
  \[Theta]t[[z]] = ArcSin[(Sin[\[Theta]i]*(n1[[z]]/n2[[z]]))];
  rs[[z]] = -Sin[\[Theta]i - \[Theta]t[[z]]]/
    Sin[\[Theta]i + \[Theta]t[[z]]]; 
  rp[[z]] = 
   Tan[\[Theta]i - \[Theta]t[[z]]/Tan[\[Theta]i + \[Theta]t[[z]]];
    Rs[[z]] = rs[[z]]*SuperStar[rs[[z]]];
    Rp[[z]] = rp[[z]]*SuperStar[rp[[z]]];
    Ravg0[[z, 2]] = (Rs[[z]] + Rp[[z]])/2;
    Ravg0[[z, 1]] = wavelength[[z]];
ListPlot[Ravg0, PlotRange -> All, Joined -> True]

Some of the errors I am recieving:

Set::partd: Part specification Ravg0[[z,2]] is longer than depth of object.
Set::partd: Part specification Ravg0[[z,1]] is longer than depth of object.
Set::partd: Part specification Ravg0[[z,2]] is longer than depth of object.
General::stop: Further output of Set::partd will be suppressed during this calculation.
Set::partw: Part 3 of {i,i} does not exist.
Set::partw: Part 3 of {i,i} does not exist.
Set::partw: Part 4 of {i,i} does not exist.

Again, I'm completely lost as to what any of this means, and I know very little about mathematica, so any help would be appreciated.


For the purpose of this example I'll assume you are a literal new user of Mathematica and have no experience. So this answer is a bit of a "hack" to get you "in gear" as quickly as possible.

You have your csv as described. To make this very easy, just go ahead in your editor of choice and make a header row for the columns of your CSV as you've indicated; ie, Col1 header = Wavelength, Col2 header = N1, Col3 header = N2. Save this as a .csv

You can now use SemanticImport to pull in your data.

imported = SemanticImport["~/example.csv"]

This should pull in a Dataset object for you. As a new user of Mathematica, you may find this more easy to get going. I've just included an example of how this import might look with some dummy data

enter image description here

Using Dataset you should now be able to apply the transforms you want to each of your columns. To get started, see this answer:

Applying a function to a column of a dataset

As well as:



Once you've done this, it's very easy to make the plots you're after; just extract the rows and columns you want and supply them directly to ListPlot. For example, if I wanted to plot all the rows of N1 against Wavelength, I would simply call:

ListPlot[imported[[All, {"Wavelength", "N1"}]], Joined -> True]

Which would produce the following:

enter image description here

Dataset constructs can be incredibly powerful and, alongside the Unit / Quantity frameworks to specify (and work with) the units in your data you can end up with some pretty neat applications.

Also, as a side note fora new user, Mathematica / WL's functional style makes life considerably easier than writing procedural code in almost all circumstances. Check out this introductory video:


or just Google around to find some excellent examples and tutorials. You will be surprised how simple it is to produce what you want without having to write loops etc.


Below is another answer in the spirit of this answer using WFR's RandomTabularDataset.

Make a random dataset:

dsRandomData = 
   "RandomTabularDataset"][{200, {"WaveLength", "N1", "N2"}}, 
  "Generators" -> {NormalDistribution[100, 40], RandomReal[{0, 100}, #] &, RandomReal[{1000, 1300}, #] &}]

enter image description here

Plot "Nx" vs "WaveLength":

ListPlot[dsRandomData[All, {"N1", "WaveLength"}]]
ListPlot[dsRandomData[All, {"N2", "WaveLength"}]]

enter image description here


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.