Velocity vector plot with empty cell array
古いコメントを表示
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?
採用された回答
その他の回答 (1 件)
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 件のコメント
Tessa Kol
2020 年 9 月 24 日
Adam Danz
2020 年 9 月 24 日
Also, I commented-out the first conditional in my answer because I realized iwant2 is a cell array and that conditional test will not work with cell arrays.
There are too many undefined variables. I can troubleshoot further if you provide something I can run on my end.
Tessa Kol
2020 年 9 月 25 日
If I could work with a minimal working example that reproduces the problem it would probably take a couple of minutes to see the problem. But without that, I'm required to read lines of code, guess the values of variables and their sizes, and I have to reverse engineer what you're doing which takes much more time that I unfortunately don't have today.
Tessa Kol
2020 年 9 月 25 日
Adam Danz
2020 年 9 月 25 日
N_run, col, and possibly other variables are also not defined.
Tessa Kol
2020 年 9 月 25 日
Adam Danz
2020 年 9 月 25 日
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
2020 年 9 月 25 日
カテゴリ
ヘルプ センター および File Exchange で Data Type Identification についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



