How to create a matrix for plotting surf(x,y,z) from multiple .dat files?
1 view (last 30 days)
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?
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
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 :)
See Also
Categories
Find more on Surface and Mesh Plots 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!