How to plot flow data from an orifice in MATLAB without overlapping the plots?

In summary, the conversation discusses studying a flow through an orifice and creating two tables for different downstream distances. The data for each table is in CSV format and will be used to plot the speed distribution over the vertical width profile for each distance. The algorithm to do this involves optimizing the given table, reading the velocity data, computing the speed, and storing the speed and width values. The code for this process is provided, but the issue is automating it for 1000 rows. A possible solution using structs is suggested, and a code that has been developed so far is shared.
  • #1
JD_PM
1,131
158
TL;DR Summary
I am aimed at overlapping the plots for the speed distribution over the vertical width profile (which is 0.015 m long; highlighted in red) for two downstream, horizontal distances [w.r.t. the orifice]: 0.01 m and 0.03 m.

How to modify my code such that it swaps all row data?
I am studying a flow going through an orifice.

I am aimed at overlapping the plots for the speed distribution over the vertical width profile (which is 0.015 m long; highlighted in red) for two downstream, horizontal distances [w.r.t. the orifice]: 0.01 m and 0.03 m.

Screenshot (186).png


The result should look as follows (the highest peak corresponds to 0.01m width profile)

1mm3mmOverlappingPlots.png

I have the data for both downstream distances (in csv format, here you can find it https://drive.google.com/drive/folders/1CgeDuCahVZvLXXkNkGWlPg8lSGyurmI3).

Let me create two tables, first related to 0.01m data and second to 0.03 m. For the sake of simplicity I only include three row data. The columns labeled - contain irrelevant data for this particular problem.

0.01m data

U0U1U2--XYZ
-0.0443263.0829e-050.001639411.5e-050.010-0.007485
-0.0886556.1659e-050.00327913e-050.010-0.00747
-0.132989.2489e-050.004918414.5e-050.010-0.007455

0.03m data

U0U1U2--XYZ
-0.11972-0.00036133-0.001235511.5e-050.030-0.007485
-0.23945-0.00072269-0.00247113e-050.030-0.00747
-0.35918-0.001084-0.003706615e-050.030-0.007455
Let's draw our attention to how to code this in MATLAB. The algorithm I have in mind is:

0) Optimize the given table: eliminate first and last rows (the velocity there is zero due to no-slip condition on the walls)

1) Read the velocity data from the csv table and compute the speed ##U = \sqrt{U_0^2 + U_1^2 + U_2^2}## for each data row.

2) Store the speed and width values in variables.

3) Repeat 2) and 3) for the other csv file.

4) Ready to plot.

I managed to write a code that works for the three-rows-of-data tables above

Matlab:
Ai = readtable("sampleData10mm.csv"); %step 0)
A = Ai(2:1000,:); %step 0)
fComr1 = A{1:1,"U_0"}; %step 1)
sComr1 = A{1:1,"U_1"}; %step 1)
tComr1 = A{1:1,"U_2"}; %step 1)
fComr2 = A{2:2,"U_0"}; %step 1)
sComr2 = A{2:2,"U_1"}; %step 1)
tComr2 = A{2:2,"U_2"}; %step 1)
fComr3 = A{3:3,"U_0"}; %step 1)
sComr3 = A{3:3,"U_1"}; %step 1)
tComr3 = A{3:3,"U_2"}; %step 1)

Ur1 = sqrt(fComr1^2 + sComr1^2 + tComr1^2); %step 1)
Ur2 = sqrt(fComr2^2 + sComr2^2 + tComr2^2); %step 1)
Ur3 = sqrt(fComr3^2 + sComr3^2 + tComr3^2); %step 1)

speed = [0;Ur1;Ur2;Ur3]; %step 2)
width = [0;A{1:1,"Points_2"}+0.0075;A{2:2,"Points_2"}+0.0075;A{3:3,"Points_2"}+0.0075]; %step 2); Note I added +0.0075 because we want to have data from 0 to 0.016 mm in the x axis

Bi = readtable("sampleData30mm.csv"); %step 3)
B = Bi(2:1000,:); %step 3)
fComr1_30mm = B{1:1,"U_0"};  %step 3)
sComr1_30mm = B{1:1,"U_1"};  %step 3)
tComr1_30mm = B{1:1,"U_2"};  %step 3)
fComr2_30mm = B{2:2,"U_0"};  %step 3)
sComr2_30mm = B{2:2,"U_1"};  %step 3)
tComr2_30mm = B{2:2,"U_2"};  %step 3)
fComr3_30mm = B{3:3,"U_0"};  %step 3)
sComr3_30mm = B{3:3,"U_1"};  %step 3)
tComr3_30mm = B{3:3,"U_2"};  %step 3)
Ur1_30mm = sqrt(fComr1_30mm^2 + sComr1_30mm^2 + tComr1_30mm^2); %step 3)
Ur2_30mm = sqrt(fComr2_30mm^2 + sComr2_30mm^2 + tComr2_30mm^2); %step 3)
Ur3_30mm = sqrt(fComr3_30mm^2 + sComr3_30mm^2 + tComr3_30mm^2); %step 3)
speed_30mm = [0;Ur1_30mm;Ur2_30mm;Ur3_30mm]; %step 3)
width_30mm = [0;B{1:1,"Points_2"}+0.0075;B{2:2,"Points_2"}+0.0075;B{3:3,"Points_2"}+0.0075]; %step 3)

figure %step 4)
plot(width,width_30mm,speed,speed_30mm) %step 4)
legend({'0.01 m', '0.03 m'},'Location','northwest') %step 4)
xlabel('width [m]') %step 4)
ylabel('U magnitude [m/s]') %step 4)
axis square; %step 4)
grid on; %step 4)

The issue I have is that I do not see how to write a code that does it for the 1000 rows. I guess there has to be a way of automatizing the whole process...

I am quite sure the code can be written more efficiently (it's my first MATLAB code ever!), so please feel to add suggestions :)

Thank you! :biggrin:

PS: Shout out to the Moderation Staff for asking me to write more details! :smile:
 
Physics news on Phys.org
  • #2
this looks pretty straight-forward. i'll give you an idea, maybe not the whole clean code, but enough to point you to a viable solution

i use structs fairly often for things like this, though probably not the most efficient.

Matlab:
data=struct(); %creates struct
for i=1:length(A);   %from 1 to size of table
data(1).u0(i)=A{i:i,"U_0"};    %speeds at first line
data(1).u1(i)=A{i:i,"U_1"};
data(1).u2(i)=A{i:i,"U_2"};
data(2).u0(i)=B{i:i,"U_0"};    %speeds at second line
data(2).u1(i)=B{i:i,"U_1"};
data(2).u2(i)=B{i:i,"U_2"};
data(1).mag=(data(1).u0(i)^2+data(1).u1(i)^2+data(1).u2(i)^2)^.5   %calculates magnitude for every point
data(2).mag=(data(2).u0(i)^2+data(2).u1(i)^2+data(2).u2(i)^2)^.5

data(1).z(i)=A{i:i,"Z"};   %gathers the z points into a vector

end
plot(data(1).z, data(1).mag);
hold on
plot(data(1).z, data(2).mag);

edit:
--------
Honestly, it's pretty quick and easy to do this all in excel...
 
Last edited by a moderator:
  • Like
Likes JD_PM
  • #3
Nice, thank you! I am not familiar with structs so I will definitely learn from it.

Let me share the code I got so far

Matlab:
clc
close all
clear all
warning('OFF', 'MATLAB:table:ModifiedAndSavedVarnames')

%step 0
Ai = readtable("sampleData10mm.csv");
Bi = readtable("sampleData30mm.csv");
A = Ai(2:1000,:);
B = Bi(2:1000,:);

%step 1
fCom_10mm = A{:,"U_0"};
sCom_10mm = A{:,"U_1"};
tCom_10mm = A{:,"U_2"};
width_10mm = A{:,"Points_2"};
U_10mm = sqrt(fCom_10mm.^2 + sCom_10mm.^2 + tCom_10mm.^2);
%refined_width_10mm = [0;A(:,"Points_2")+0.0075];
%refined_width_10mm = A{:,"Points_2"+0.0075};

%step 2
fCom_30mm = B{:,"U_0"};
sCom_30mm = B{:,"U_1"};
tCom_30mm = B{:,"U_2"};
width_30mm = B{:,"Points_2"};
U_30mm = sqrt(fCom_30mm.^2 + sCom_30mm.^2 + tCom_30mm.^2);
%refined_width_30mm = [0;B(:,"Points_2")+0.0075];
%refined_width_30mm = B{:,"Points_2" + 0.0075};

%step 3
figure
plot(width_10mm,U_10mm,width_30mm,U_30mm)
%plot(refined_width_10mm,U_10mm,refined_width_30mm,U_30mm)
legend({'0.01 m', '0.03 m'},'Location','northwest')
xlabel('width [m]')
ylabel('U magnitude [m/s]')
axis square;
grid on;

Which yields

Screenshot (189).png


I am almost there. I am working on how to get the zero at the x-axis y-axis intersection:

Screenshot (190).png
 

FAQ: How to plot flow data from an orifice in MATLAB without overlapping the plots?

How do I plot a simple line graph in MATLAB?

To plot a simple line graph in MATLAB, you can use the plot function. This function takes two input arguments: the x-values and the y-values. For example, if you want to plot the points (1,2), (2,4), and (3,6), you can use the following code:

x = [1,2,3];
y = [2,4,6];
plot(x,y);

How can I customize the appearance of my plot in MATLAB?

MATLAB offers various functions to customize the appearance of your plot. Some of these functions include xlabel and ylabel to add labels to the x and y axes, title to add a title to your plot, and legend to add a legend. You can also use the grid function to add grid lines to your plot. Additionally, you can change the color, line style, and marker style of your plot using the Color, LineStyle, and Marker name-value pairs in the plot function.

How do I plot multiple data sets on the same graph in MATLAB?

To plot multiple data sets on the same graph in MATLAB, you can use the hold on command after your first plot function. This will hold the current plot and allow you to add additional plots to the same graph. You can also use the hold off command to stop holding the plot and start a new one. Alternatively, you can use the plot function with multiple input arguments, each representing a different data set. For example, plot(x,y1,x,y2); will plot both y1 and y2 against x on the same graph.

How can I save my plot as an image in MATLAB?

To save your plot as an image in MATLAB, you can use the saveas function. This function takes two input arguments: the handle of your figure and the file name. For example, if your figure handle is fig and you want to save your plot as myplot.png, you can use the following code:

saveas(fig, 'myplot.png');

How do I plot data from a CSV file in MATLAB?

To plot data from a CSV file in MATLAB, you can use the csvread function to read the data from the file and store it in a variable. Then, you can use the plot function to plot the data. For example, if your CSV file has two columns of data, you can use the following code:

data = csvread('mydata.csv');
x = data(:,1); % first column of data
y = data(:,2); % second column of data
plot(x,y);

Similar threads

Replies
8
Views
950
Replies
2
Views
3K
Replies
4
Views
3K
Replies
1
Views
929
Replies
8
Views
2K
Replies
2
Views
2K
Replies
9
Views
4K
Back
Top