Plot values of 3D matrix against a single variable

2 views (last 30 days)
I have a 4D matrix (4x4x4x6), the first three dimensions are the pressure at each of the x,y,z points in a space. The fourth dimension is the frequencies used to calculate the pressure. I want to plot the frequency response of the space. That means plot all the pressures against the frequency used to calculate them.

Accepted Answer

Image Analyst
Image Analyst on 13 Oct 2021
Try this:
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
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 = 17;
pressure = rand(4, 4, 4, 6); % Random data
[rows, columns, slices, freq] = size(pressure)
% Plot all pressure profiles for each (x,y,z) point.
for row = 1 : rows
for col = 1 : columns
for z = 1 : slices
% Get frequency response over all locations
p = squeeze(pressure(row, col, z, :))
% Plot this set of pressures for this particular (x,y,z) location:
plot(p, 'LineWidth', 2);
if row == 1 & col == 1 && z == 1
grid on;
xlabel('Frequency', 'FontSize', fontSize)
ylabel('Pressure', 'FontSize', fontSize)
hold on;
end
end
end
end
  2 Comments
Evagoras Kassapis
Evagoras Kassapis on 14 Oct 2021
Edited: Evagoras Kassapis on 14 Oct 2021
Yes, thank you for this, works great. How can I get it to be one line plot, like one pressure line vs frequency. Do I average the pressures at each frequency and then plot it?

Sign in to comment.

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!