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

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

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?
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

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)

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

Thank you very much Nielsen.
Actually I am very new to matlab. Can you please explain me how to include all 60 files together. So that the programme can read those files?
Thanks a lot.
Regards
Rejaul
Hey Rejaul.
First, put all your files in the same folder. Name them in numerical (or alphabetical, or whichever you choose) ascending order - this way, when matlab runs through the loop 60 times you will get the first file in the first loop, 2nd file in 2nd loop and so on.
When you run the script example I gave, it should open a file selection dialog. Navigate to the folder you placed all your text files in, and select all of them at once. The script will run through all files, make your matrix, and plot the result.
Then, if everything looks right, you can save the resulting workspace as a .mat file so it is faster to open another time :)
multiselect_example.png
When I run it, i get this error-
[Name,Path]=uigetfile({'*.txt*'}],'MultiSelect','on'); %select your 60 files
Error: Invalid expression. When calling a function or indexing a variable, use parentheses.
Otherwise, check for mismatched delimiters.
Yes i got the error. That should be the first ")" bracket instead of "]" in the first linne.
Thanks a lot the script in working. Thanks alot.
I would like the plot to be like the attached image. Not exaclt like the output i am having now.
Well, if you have the data loaded in, you can play around a little with the plot options. What does the figure you get look like now?
shading interp
could be an option you might like.
I want the figure look like the attached one. Which is only the top view of the 3D plot i am getting.
Thanks
Just turn your 3D plot with the mouse until you get a top-down view :)
Or set
view(0,90);
Which will do the same.
Thanks a lot for helping me. It was really helpful.
Thanks and kind regards
Rejaul
Dear Nielsen,
Can you please help me with one more thing.
How can i select partial rows? Like if i only want to plot row 30-300 only, not the whole data. Rest will remain same.
thanks a lot in advance.
regards
Rejaul
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 :)
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.

Categories

Asked:

on 5 Feb 2020

Commented:

on 11 Feb 2020

Community Treasure Hunt

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

Start Hunting!