MATLAB Answers

Velocity vector plot with empty cell array

13 views (last 30 days)
Tessa Kol
Tessa Kol on 22 Sep 2020
Answered: Tessa Kol on 28 Sep 2020 at 18:56
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?

  0 Comments

Sign in to comment.

Accepted Answer

Tessa Kol
Tessa Kol on 28 Sep 2020 at 18:56
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.

  0 Comments

Sign in to comment.

More Answers (1)

Adam Danz
Adam Danz on 22 Sep 2020
Edited: Adam Danz on 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 Comments

Show 6 older comments
Tessa Kol
Tessa Kol on 25 Sep 2020 at 16:49
N_run = 2 and length(col) = 7
Adam Danz
Adam Danz on 25 Sep 2020 at 16:54
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 on 25 Sep 2020 at 18:03
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.

Sign in to comment.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!