How can I fix "error Using plot" "Data cannot have more than two dimensions"?

171 views (last 30 days)
I have a user defined function which i refer to as TruncatedVol, which i refer to in my script:
function vol= TruncatedVol(R1, R2,H);
%Converts to vector inputs to meshgrid in order to allow element by element mutliplication
[r1,r2,H1] = meshgrid(R1,R2,H);
%Finds the height if the cone were not truncated and sets that value as T
% h is the distance from the center of the smaller radius to the the tip of the cone if the cone were not truncated
h = (H1.*r2)./(r1-r2);
T = H1 + h;
%Outputs the volume of the frustrum
vol = (pi/3)*[(T.*r1.^2) - (h.*r2.^2)];
end
Script 2 : Titled Project 1
%Houskeeping
clc
clear all
%test 1
%R1 = 5, R2 = 4, H= 10
Test1 = TruncatedVol(5, 4,10);
%test 2: One input has vector
%R1 = 5:.25:7, H = 10, R2 = 4
Test2 = TruncatedVol(5:.25:7, 4,10);
%test 3: Two inputs are vectors
%R1 = 5:.25:7, H = 7:.25:10, R2 = 4
Test3 = TruncatedVol(5:.25:7,4,7:.25:10);
%define values for plot for Large Radius is constant, height constant, and smaller radius
%changes
rp =(1:.1:4);
v1= TruncatedVol(4,rp,10);
% define values for plot for R and r are constant, h changes
Hp = (1:.1:10);
v2= TruncatedVol(5,4,1:.1:10);
%Makes the plots onto Figures
figure
%Plot 1: R1 is changing
subplot(2,1,1)
plot(rp,v1,'-g')
xlabel('Radius of Smaller End(in)')
ylabel('Volume (in^3)')
subplot(2,1,2)
plot(Hp,v2,'-b')
xlabel('Height of frustrum(in)')
ylabel('Volume (in^3)')
In my second script file, I refrence this function, and can get volumes for various vectors when they're R1 and R2. Problems are with H, which Test 3 gives me a 1x9x13 , and gives me the error
"Error using plot
Data cannot have more than 2 dimensions.
Error in Project1 (line 34)
plot(Hp,v2,'-b')"
I want to make a plot of v2 as as Hp changes, but that error appears, how do i fix this?

Answers (1)

KSSV
KSSV on 15 Oct 2020
Your V2 is a 3D array..it is of size 1*1*91.....Use:
plot(Hp,squeeze(v2),'-b')
Check your code where V2 is generated and try to make it as 1*91

Categories

Find more on Testing Frameworks in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!