How to create a matrix for plotting surf(x,y,z) from multiple .dat files?
Show older comments
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
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
on 5 Feb 2020
Accepted Answer
More Answers (1)
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
Rejaul Sk
on 10 Feb 2020
Jakob B. Nielsen
on 10 Feb 2020
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 :)

Rejaul Sk
on 10 Feb 2020
Rejaul Sk
on 10 Feb 2020
Jakob B. Nielsen
on 10 Feb 2020
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.
Rejaul Sk
on 10 Feb 2020
Jakob B. Nielsen
on 10 Feb 2020
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.
Rejaul Sk
on 10 Feb 2020
Rejaul Sk
on 11 Feb 2020
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
on 11 Feb 2020
Categories
Find more on Vector Fields in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!