How to create a matrix for plotting surf(x,y,z) from multiple .dat files?

2 views (last 30 days)
I have 60 .dat files. From which i want to creat a matrix for Z values. The values are a specific column from each of these .dat file. Similarly Y values is the 1st column from each of these .dat file. I want to specify the x values manualy. Then want to plot the variantion of Z as a surface with color.
Any help will be highly appreciated
  2 Comments
Jakob B. Nielsen
Jakob B. Nielsen on 5 Feb 2020
Can you post an example of one of your .dat files? You say "The values are a specific column from each of these .dat file." - do you mean multiple columns? In order to make a surface plot, you need two vectors X and Y of size greater than 1, and a matrix Z with dimensions X x Y.
Or is the idea that every data file has the same first column, which is your Y, and then you need a matrix, Z, of a specific column of all 60 .dat files?
Rejaul Sk
Rejaul Sk on 5 Feb 2020
Here I attached one such file.
The first column is the Y values. Which is same in all 60 files.
I want the column 7(LIX 1 omega) to be treated as Z values. I want to creat a matrix of 512 rows and 60 columns. 512 will be the data from the column 7 of each file. 60 is the no of files.
The x values will be 0-59.
Them I would like to plot surf(x,y,z) to see the variation of Z in the color plot
Thanks a lot in advance.

Sign in to comment.

Accepted Answer

Rejaul Sk
Rejaul Sk on 5 Feb 2020
Here I attached one such file.
The first column is the Y values. Which is same in all 60 files.
I want the column 7(LIX 1 omega) to be treated as Z values. I want to creat a matrix of 512 rows and 60 columns. 512 will be the data from the column 7 of each file. 60 is the no of files.
The x values will be 0-59.
Them I would like to plot surf(x,y,z) to see the variation of Z in the color plot
Thanks a lot in advance.

More Answers (1)

Jakob B. Nielsen
Jakob B. Nielsen on 6 Feb 2020
I hope your data files are in order! :)
First, get arrays of your filenames, and then create a loop withinwhich you use the fullfile function to get the specific path to each file, read the file, and add the relevant column to your matrix. Of course, your files must be in order, or you will get the wrong order in the matrix too.
[Name,Path]=uigetfile({'*.txt*'}],'MultiSelect','on'); %select your 60 files
Filecount=size(Name,2);
Z=zeros(512,60); %always a good idea to initialize
for i=1:Filecount
entirefile=fullfile(Path,Name{i});
A=importdata(entirefile,' ',17); %syntax is importdata(filename,delimiter,headerlines) and in your case, delimiter is tab, and you have 17 lines of text before your data starts. I encourage you to read the documentation for the function (type doc importdata in the command window)
%from here, simply assign:
Z(:,i)=A.data(:,7); %the ith column in Z will be the 7th column in the current datafile which will change every loop
end
%Then set your Y (just using the last loop since they are all identical:
Y=A.data(:,1);
%and your X:
X=0:1:59;
%finally, your surface:
surf(X,Y,Z);
  11 Comments
Jakob B. Nielsen
Jakob B. Nielsen on 11 Feb 2020
Hi Rejaul,
You just specify it in the indexes. E.g.
surf(X,Y(30:300),Z(30:300,:));
The syntax here being you plot all of X, the 30th to 300th of Y, and then Z(30:300,:)) means all you plot row 30 to 300, but the : in the column index means you plot everything in the column of those rows. (If theres an error try Z(:,30:300)); instead, I forgot your dimensions :P )
Also, if you can go ahead and accept the answer above, the whole thread here will be moved into the answered questions section :)
Rejaul Sk
Rejaul Sk on 11 Feb 2020
Dear Nielsen,
Thanks a lot. I can finally creat the matrix i wanted. I am also able to plot it in 2d color plot.
But my plot is very noisey. As you can see if i plot on cloumn 7( Z matrix) vs column 2( Y values) from each flle it is very noisy(see attachment)
Can you please help me to add some command to smooth/filtered each of these 60 similar curves first then creat the Z matrix?
Basically first i want to filter each curve( coulmn 7 vs column 2) of all 60 files.
Then i would like to creat the Z matrix and plot it in 2D color plot.
Thanks a lot in advance.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!