Assigning positions to multiple figures along-z-axis.

hi,
I have a code that generates multiple contour filled plots using a for loop. i want to position the each result at different positions along the z-axis as when i am running the code, it overwrites the previous image and not able to change the position along-z-axis. here is the code. I have tried hold function but how can i hold the plots one on top of the other. As i want multiple 2D contour plots in one matlab figure seperated by distance along the z-axis.
Or if there is a way to access already saved contourfilled plots and plotting them on top of other.
Code:
figure(1);
for i=1:1:7
hold on
slice= MAG_uwb_scatt_incoh_reconstr_diff_OLD_normalised(:,:,i);
x=X_reconstr(:,:,i);
y=Y_reconstr(:,:,i);
limit=[0.6 1];
contourf(x,y,slice,'LineStyle' , 'none');
view(3)
caxis(limit)
colorbar
grid on
axis normal
end
using this code i am getting this result.
but instead of this i want results like this and i want to position them along -zaxis. Yours help will be greatly appreciated, i would be greatfull if you suggest me solution to this. thanks.

 Accepted Answer

contourf() always represents the data at z = 0.
You need to do something like this:
fig = figure(1);
ax = gca(fig);
hold(ax, 'on')
z_per_slice = 5;
z_base = 1;
for i=1:1:7
slice= MAG_uwb_scatt_incoh_reconstr_diff_OLD_normalised(:,:,i);
x=X_reconstr(:,:,i);
y=Y_reconstr(:,:,i);
limit=[0.6 1];
tform = hgtransform(ax);
contourf(tform, x,y,slice,'LineStyle' , 'none');
tform.Matrix = makehgtform('translate', [0, 0, z_base + z_per_slice*(i-1)]);
end
hold(ax, 'off')
view(3)
xlim(ax, 'auto')
ylim(ax, 'auto')
zlim(ax, 'auto')
caxis(limit)
colorbar(ax)
grid(ax, 'on')
axis(ax, 'normal')

6 Comments

Thanks for your answer really appreciate that, i have tried that and error message showed up for real input arguments for contourf.
Error using contourf (line 55)
Input arguments must be real. Use the function REAL to get the real part of the inputs.
You forgot to attach the complete error message. Walter did not provide 55 lines. You need to include ALL the red text, not just part of it.
Now, why is your input variable complex? Did you take the real part of it, like it said, and try that?
the complete error message is as follow:
Error using contourf (line 55)
Input arguments must be real. Use the function REAL to get the real part of the inputs.
Error in finalcode (line 589)
contourf(tform, x,y,slice,'LineStyle' , 'none');
I didn't had this error before using transform (tform) as suggested by Walter in Contourf command. the rest of the input arguments for contourf are real.
But you need to continue debugging if youi want to solve it. who knows when Walter will get back.
Most people would have then looked at the variables to see what they are and what's complex. The next step most people would do (and you probably will too after viewing the debugging tutorials) is to look in the workspace panel at those input variables or do
whos tform
whos x
whos y
whos slice
Thanks. will try that and get back to you if anything comes up.
Note:
zlim auto did not work to figure out the range of transformed coordinates
legend show showed only one empty legend entry, so the handles had to be accumulated.
M = 20; N = 30; S = 7;
MAG_uwb_scatt_incoh_reconstr_diff_OLD_normalised = sort(.5 + .6 * rand(M,N,S),2);
[X_reconstr, Y_reconstr, ~] = ndgrid(linspace(0,1,M), linspace(0,pi/2,N), 1:S);
fig = figure(1);
ax = gca(fig);
z_per_slice = 5;
z_base = 1;
limit=[0.6 1];
for i=1:1:S
slice= MAG_uwb_scatt_incoh_reconstr_diff_OLD_normalised(:,:,i);
x=X_reconstr(:,:,i);
y=Y_reconstr(:,:,i);
tform = hgtransform(ax);
hold(ax, 'on')
[~, h(i)] = contourf(x,y,slice,'LineStyle' , 'none', 'parent', tform, 'displayname', string(i));
tform.Matrix = makehgtform('translate', [0, 0, z_base + z_per_slice*(i-1)]);
end
hold(ax, 'off')
view(3)
xlim(ax, 'auto')
ylim(ax, 'auto')
caxis(limit)
colorbar(ax)
grid(ax, 'on')
axis(ax, 'normal')
legend(h)
zlim(ax, [z_base z_per_slice*(S-1)+z_base])

Sign in to comment.

More Answers (1)

You can probably adapt this code:
% Takes an RGB image and stacks the separate color channels at different Z levels.
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 13;
filename = 'peppers.png';
rgbImage = imread(filename);
% Extract the individual red, green, and blue color channels.
redChannel = im2double(rgbImage(:, :, 1));
greenChannel = im2double(rgbImage(:, :, 2));
blueChannel = im2double(rgbImage(:, :, 3));
H(1) = slice(repmat(redChannel,[1 1 2]),[],[], 1); %slice() requires at least 2x2x2
set(H(1),'EdgeColor','none') %required so image isn't just an edge
hold on
H(2) = slice(repmat(greenChannel,[1 1 2]),[],[], 2); %slice() requires at least 2x2x2
set(H(2),'EdgeColor','none') %required so image isn't just an edge
H(3) = slice(repmat(blueChannel,[1 1 3]),[],[], 3); %slice() requires at least 2x2x2
set(H(3),'EdgeColor','none') %required so image isn't just an edge
hold off
colormap(gray(256))
axis ij
caption = sprintf('R, G, and B Color Channels of %s', filename);
title(caption, 'FontSize', fontSize);
% Put up legend that says what slice is what color channel.
legend('B', 'G', 'R')

Products

Release

R2018b

Community Treasure Hunt

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

Start Hunting!