how to create a mean velocity matrix from multiple .dat files and plot the mean profile?
2 次查看(过去 30 天)
显示 更早的评论
Ernest Adisi
2018-7-31
I have a folder of x no of *.dat files. So far I've imported 1 file and reshaped the columns to get matrices for each column. e.g for 1 dat file:
filename='B00049.dat';
delimiterIn= ' ';
headerlinesIn = 3;
A= importdata(filename,delimiterIn,headerlinesIn);
%% Matrices%%%%
c = 214;%columns i
r = 134;%rows j
dt= 0.0005;
x=reshape(A.data(:,1),r,c);
y=reshape(A.data(:,2),r,c);
u=reshape(A.data(:,3),r,c);
v=reshape(A.data(:,4),r,c);
I now want to import multiple files and creat a mean velocity matrix fro u and v then plot a profile. How can this be done ? Thanks
2 个评论
Ernest Adisi
2018-7-31
thanks for the response, i'm trying to create a path to the folder containing all the files then perform a loop in order to make the 4 matrices for each file, then finally create a mean velocity matrix from the average of the last two columns in the 2 dat files. Hope that makes sense
采纳的回答
jonas
2018-7-31
编辑:jonas
2018-7-31
files=dir('*.dat') %If files are in current dir, otherwise enter entire path
%%Preallocate some cells
x=cell(1,numel(files))
y=cell(1,numel(files))
u=cell(1,numel(files))
v=cell(1,numel(files))
%%Loop over all files
for i=1:numel(files)
filename=files(i).name
delimiterIn= ' ';
headerlinesIn = 3;
A= importdata(filename,delimiterIn,headerlinesIn);
c = 214;%columns i
r = 134;%rows j
dt= 0.0005;
%%Save results
x{i}=reshape(A.data(:,1),r,c);
y{i}=reshape(A.data(:,2),r,c);
u{i}=reshape(A.data(:,3),r,c);
v{i}=reshape(A.data(:,4),r,c);
end
When you have all the data stored in cells, you can concatenate along the third dimensinon and take the average. I assume all files share the same size?
V = cat(3, v{:});
mean(V,3)
25 个评论
Ernest Adisi
2018-8-1
Yes they are. This works brilliantly. Thanks a lot, can I just ask a further question.
1. for V = cat(3, v{:}); has it concatenated the V matrices so like they are stacked like a 'stack of waffles' for example, then calculates a mean matrix ? and the 3 what does it do please? don't understand the matlab explanation of it
Ernest Adisi
2018-8-1
alright thanks I understand that now. lastly, i'm just wondering what the 'v{:}' in the command V = cat(3, v{:}); is telling matlab
Ernest Adisi
2018-8-1
and also, from the mean matrix can how do i plot a mean profile. i don't if its a contour plot or something else? so it display the different velocity profiles in the regions
Stephen23
2018-8-1
" i'm just wondering what the 'v{:}' in the command V = cat(3, v{:}); is telling matlab"
jonas
2018-8-1
It's difficult for me to plot because I am not really familiar with your data. Nevertheless, I made an attempt. Is that what you want?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/193261/image.jpeg)
[Xq Yq]=meshgrid(0:6:max(x{1}(:)),0:6:max(y{1}(:)));
Vq=griddata(x{1}(:),y{1}(:),V_mean(:),Xq,Yq);
contourf(Xq,Yq,Vq)
Ernest Adisi
2018-8-1
Yes, its that, thank you v.much Jonah. i'm just trying to understand the code but that seems similar to what I need to achieve. Thank you
jonas
2018-8-1
You're welcome. Attempting to understand the code is exactly what you should be doing, so continue with that. The MATLAB documentation is extremely helpful!
The plotting is fairly simple, but looks complex. Just read about meshgrid (creates a grid) and griddata (interpolates scattered data to the grid).
Ernest Adisi
2018-8-3
%% plot mean velocities %%%% V-mean%% [Xq Yq]=meshgrid(0:2:max(x{1}(:)),0:2:max(y{1}(:))); Vq=griddata(x{1}(:),y{1}(:),V_m(:),Xq,Yq);
f1=figure; contourf(Xq,Yq,Vq); xlabel({'x(m)'}) ylabel({'y(m)'}) title('V-mean velcity profile') hcb= colorbar title(hcb,'Velocity[m/s]')
%%%% U-mean%% [Xq Yq]=meshgrid(0:2:max(x{1}(:)),0:2:max(y{1}(:))); Uq=griddata(x{1}(:),y{1}(:),U_m(:),Xq,Yq);
f2=figure; contourf(Xq,Yq,Uq) xlabel({'x(m)'}) ylabel({'y(m)'}) title('U-mean velcity profile') hcb= colorbar title(hcb,'Velocity[m/s]')
this seems to have a problem with the plot, do you have an idea why please?
jonas
2018-8-3
Please make sure your code is formated like this:
[Xq Yq]=meshgrid(0:2:max(x{1}(:)),0:2:max(y{1}(:)));
Vq=griddata(x{1}(:),y{1}(:),V_m(:),Xq,Yq);
f1=figure;
contourf(Xq,Yq,Vq);
xlabel({'x(m)'}) ylabel({'y(m)'})
title('V-mean velcity profile')
hcb= colorbar
title(hcb,'Velocity[m/s]')
%%%%U-mean%%
[Xq Yq]=meshgrid(0:2:max(x{1}(:)),0:2:max(y{1}(:)));
Uq=griddata(x{1}(:),y{1}(:),U_m(:),Xq,Yq);
f2=figure;
contourf(Xq,Yq,Uq)
xlabel({'x(m)'})
ylabel({'y(m)'})
title('U-mean velcity profile')
hcb= colorbar
title(hcb,'Velocity[m/s]')
Seems fine to me, provided that the grid should start at x=0 and y=0. Otherwise, change to:
[Xq Yq]=meshgrid(min(x{1}(:)):2:max(x{1}(:)),min(x{1}(:)):2:max(y{1}(:)));
Other than that, the code looks fine at first glance. What is the error message?
Ernest Adisi
2018-8-9
Hey, can i ask another question please. I trying to change the orientation of the profile,like 90 degree rotation, could you show me how its done please
jonas
2018-8-9
What do you mean? Do you want to just change the view? Then change the first value in the view pair. For example:
set(gca,'view',[90 90]) %
...or do you want to rotate the actual Z-matrix 90 degrees?
B = rot90(A)
this will however crash your script unless you have a square matrix. You probably just want to change the view.
Ernest Adisi
2018-8-9
like as you see in the plot above, the flow direction is top to bottom , but i want it left to right so rotation in z axis. while keeping the x and y the same.
Ernest Adisi
2018-8-9
for example, this rotated anticloclwise 90 degrees but everything else kept the same
jonas
2018-8-9
I dont understand. Do you want the flow direction to be ~0.6-0.7 m in your latest image?
Ernest Adisi
2018-8-9
you're right. It worked. Thank you so much again for all the help. Appreciate it
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Performance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)