Velocity vector plot with empty cell array

1 Ansicht (letzte 30 Tage)
Tessa Kol
Tessa Kol am 22 Sep. 2020
Beantwortet: Tessa Kol am 28 Sep. 2020
With the following code I made a velocity vector plot over time.
%% Velocity vector plot
close all
x_cent = mean(X(1,1:2));
z_cent = mean(Z(1:2,1));
Nx2 = 49;
Nz2 = 49;
bins_x2 = linspace(x_cent,-x_cent,Nx2);
% Bins defined over the height of the silo
bins_z2 = linspace(0.6-z_cent,z_cent,Nz2);
[X2,Z2] = meshgrid(bins_x2,bins_z2);
for i = 1:N_run
for t = 1:length(col)
vx_avr{t,i} = cellfun(@(x)mean(x(:,1)),iwant2(:,:,t,i));
vz_avr{t,i} = cellfun(@(x)mean(x(:,2)),iwant2(:,:,t,i));
end
end
for i = 1:N_run
for t = 1:5
vx_rot{t,i} = rot90(vx_avr{t,i},2);
vz_rot{t,i} = rot90(vz_avr{t,i},2);
end
end
for i = 1:N_run
figure(i)
hold on
for t = 1:length(col)
quiver(X2,Z2,vx_rot{t,i},vz_rot{t,i})
end
end
Below you see an example of a velocity vector plot at t = 4 (which represents 2 seconds)
However, when t = 6 (representing 3 seconds) there are no velocity vectors left resulting in an empty iwant2. When I use the function cellfun to take the mean I get of course an error, because it's empty. How can I make sure that matlab ignores the empty iwant2 and just plot an empty quiver instead?

Akzeptierte Antwort

Tessa Kol
Tessa Kol am 28 Sep. 2020
Solved it myself with an if else statement, see code below.
for i = 1:N_run
for t = 1:length(col)
if cellfun(@isempty,iwant2(:,:,t,i))
vx_avr{t,i} = zeros(Nz2,Nx2);
vz_avr{t,i} = zeros(Nz2,Nx2);
else
vx_avr{t,i} = cellfun(@(x)mean(x(:,1)),iwant2(:,:,t,i));
vz_avr{t,i} = cellfun(@(x)mean(x(:,2)),iwant2(:,:,t,i));
end
end
end
Thank you Adem Danz for your suggestions, it really helped me in finding the final solution to this problem.

Weitere Antworten (1)

Adam Danz
Adam Danz am 22 Sep. 2020
Bearbeitet: Adam Danz am 24 Sep. 2020
We can't run your code due to missing variables but here are two general suggestions.
Before before we get to that,
  • always preallocate loop variables as shown below
  • use numel() instead of length()
1. If iwant2 is empty, just skip to the next loop and check for empties on all other loops, too. See the 3 conditionals that check for empty arrays.
% Pre allocate loop vars!
vx_avr = cell(numel(col), N_run);
vz_avr = vx_avr;
vx_rot = vx_avr;
vz_rot = vx_avr;
for i = 1:N_run
for t = 1:numel(col)
%if isempty(iwant2(:,:,t,i)); update: this won't work
% continue
%end
vx_avr{t,i} = cellfun(@(x)mean(x(:,1)),iwant2(:,:,t,i));
vz_avr{t,i} = cellfun(@(x)mean(x(:,2)),iwant2(:,:,t,i));
end
end
for i = 1:N_run
for t = 1:5
if isempty(vx_avr{t,i})
continue
end
vx_rot{t,i} = rot90(vx_avr{t,i},2);
vz_rot{t,i} = rot90(vz_avr{t,i},2);
end
end
for i = 1:N_run
figure(i)
hold on
for t = 1:numel(col)
if isempty(vz_rot{t,i})
continue
end
quiver(X2,Z2,vx_rot{t,i},vz_rot{t,i})
end
end
2. If you want to plot "empty" quiver objects, use the same example above but change the last condition to
for i = 1:N_run
figure(i)
hold on
for t = 1:numel(col)
if isempty(vz_rot{t,i})
quiver(nan,nan)
else
quiver(X2,Z2,vx_rot{t,i},vz_rot{t,i})
end
end
end
  9 Kommentare
Adam Danz
Adam Danz am 25 Sep. 2020
There are other missing vars, too. One example is expData_struc.
You could clear your workspace and try to run only the code you provided so you can see which vars are not defined.
Tessa Kol
Tessa Kol am 25 Sep. 2020
expData_struc is very big (over 1 GB), so that is problematic. I thought that sending the iwant2 in the .mat file iit could reproduce the problem enough. Since I get errors with the following code:
vx_avr = cell(length(col), N_run);
vz_avr = vx_avr;
for i = 1:N_run
for t = 1:length(col)
if isempty(iwant2(:,:,t,i))
continue
end
vx_avr{t,i} = cellfun(@(x)mean(x(:,1)),iwant2(:,:,t,i));
vz_avr{t,i} = cellfun(@(x)mean(x(:,2)),iwant2(:,:,t,i));
end
end
For that piece of code I thought you only need the vars iwant2, length(col) and N_run.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Vector Fields finden Sie in Help Center und File Exchange

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by