0

In my project I basically need to plot 3D color coded surface graphs using MATLAB taking input from multiple .dat files ' kind of a text file'. each .dat file contains a list of values of REFLECTION R wrt to launch angle theta. each file is for a specific value of lambda ( wavelength). so by taking multiple .dat files I need to plot. 3D graph in MATLAB wherein x axis is theta, y axis is REFLECTION R and z would be lambda which is different for different files. Each .dat files gives me a 2D graph of theta vs REFLECTION R. each 2D graph varies from another based on values of lambda. I somehow need to use different values of lambda as z axis to draw a 3D graph.

In a way, in each file I have parameters x and y, and when I take multiple files, each having a different value of z, I can finally plot a 2D graph. so does anyone know how to take input from these multiple .dat files and draw a 3D graph in MATLAB?

I hope I'm clear enough, if not feel free to ask ur doubts thanks :)

  • can you post the code you currently have. For example how do you plot one of those file? – Amro Jul 25 '12 at 22:02
  • I'm very new to matlab, so just using whatever I get from internet..well see i basically have just .dat files having 2 columns, one has theta and other has total_reflection.. when i – user1552734 Jul 26 '12 at 07:21
  • I'm very new to matlab, so just using whatever I get from internet..well see i basically have just .dat files having 2 columns, one has theta and other has Reflection.. I used the import wizard of Matlab to import my file, namely tauk.dat, then I removed the heading and used only the numeric data to form a 2D array, which i made into two separate variables as follows: phi=data(:,1); r=data(:,2); plot(phi,r); This gives me a 2D plot, need 3D of many such files and their respective graphs combined. – user1552734 Jul 26 '12 at 08:07
  • the truth is I'm having trouble imagining what type of graph you want to generate. See this [plot gallery](http://www.mathworks.com/discovery/gallery.html) to get some ideas. But please be more specific how you want each variable to be plotted. (I am thinking something similar to [this one](http://www.mathworks.com/matlabcentral/fileexchange/35262-matlab-plot-gallery-line-plot-3d/content/html/Line_Plot_3D.html)) – Amro Jul 26 '12 at 16:10
  • i want a color coded 3D surface plot or contour if it is possible, or any 3D color coded plot which is possible with the current data. See basically each .dat file gives me a 2D curve or plot. So with different values of the third variable, I get different 2D curves or plots. so when i put all these graphs together one above the other based on the third parameter's value, I'll get a 3D graph, mostly it is supposed to be a surface one. – user1552734 Jul 28 '12 at 07:25

1 Answers1

1

If you can supply data, it is easier to help since it matters how your data is organized. Nonetheless, here is a shot.

I understand the question to be: if I have multiple files, each of which relates theta and R for a single value of lambda, how do I produce a 3d plot of all of these? I will assume that every file contains the same values of theta. It is a bit more complicated, but straightforward if they do not.

E.g., assume you have three files:

  • lambda=100: theta1 = [15 30 45 60 75], R1 = [49 56 61 65 67]
  • lambda=200: theta2 = [15 30 45 60 75], R2 = [41 50 57 62 66]
  • lambda=300: theta3 = [15 30 45 60 75], R3 = [34 45 53 60 65]

In this case, you can construct x and y vectors using meshgrid and concatenate the R values. Then you can plot with any number of 3d plotting commands. I show mesh.

[theta, lambda] = meshgrid(theta1, [100 200 300]);
R = [R1; R2; R3];
mesh(theta,lambda,R)
xlabel('theta')
ylabel('lambda')
zlabel('R')

example mesh plot

If I have many files, what I would do automate this by ingesting all of the files with a script that lists the contents of the directory and iterates through each of the files, but you could instead set it all up manually. This might looks something like (adapted from one of my files):

fileNameArray = dir('*.dat');
for i = 1:length(fileNameArray)
    datStruct = importdata(datStruct(i).name;
    theta(:,i) = datStruct.data(:,1);
    R(:,i) = datStruct.data(:,2);
end

This assumes the data is in columns. Putting both theta and R into matrices allows one to inspect theta and make sure that all of the entries are the same.

I also recommend checking out the 3-D Visualization and especially Creating 3-D Graphs in the MATLAB documentation. I browsed these many times when I was getting started.

Community
  • 1
  • 1
sage
  • 4,863
  • 2
  • 44
  • 47