Sub function error within main function

1 次查看(过去 30 天)
function volSAtsa(l , w, h)
vol = volume (l, w, h);
area = sides(l, w , h);
tsa = total(a1 , a2 , a3);
output = out(vol , a1 , a2, a3 , tsa);
end
function vi = volume(l , w, h)
vi = l*w*h;
end
function a = sides(l , w , h)
a1 = l*w;
a2 = w*h;
a3 = l*h;
end
function tsarea = total(a1 , a2 , a3)
tsarea= a1 +a2 + a3;
end
function results = out(a1 , a2 , a3, tsa)
fprintf(' Volume = %.2f\n ', vi)
fprintf('Area 1 = %.2f\n ', area1)
fprintf('Area 2 = %.2f\n ', area2)
fprintf('Area 3 = %.2f\n ',area3)
fprintf('Total Area = %.2f\n ' , tsarea)
end
My program keeps giving me an error at area = sides (l,w,h). I've tried many many things to by pass it but I am running in circles at this point.

回答(3 个)

Shubham Gupta
Shubham Gupta 2019-11-11
Try:
function volSAtsa(l , w, h)
vol = volume (l, w, h);
[a1, a2, a3] = sides(l, w , h);
tsa = total(a1 , a2 , a3);
out(vol , a1 , a2, a3 , tsa);
end
function vi = volume(l , w, h)
vi = l*w*h;
end
function [a1,a2,a3] = sides(l , w , h)
a1 = l*w;
a2 = w*h;
a3 = l*h;
end
function tsarea = total(a1 , a2 , a3)
tsarea= a1 +a2 + a3;
end
function out(vi,a1 , a2 , a3, tsa)
fprintf(' Volume = %.2f\n ', vi)
fprintf('Area 1 = %.2f\n ', a1)
fprintf('Area 2 = %.2f\n ', a2)
fprintf('Area 3 = %.2f\n ',a3)
fprintf('Total Area = %.2f\n ' , tsa)
end

Steven Lord
Steven Lord 2019-11-11
In general, when you're asking for help with an error you should show the full and exact text of the error message you receive. [Show all the text displayed in red.] That information may be very useful.
But in this case, I can see at least one problem. What does your sides function return? Where do you define a value for that variable inside your sides function?
In addition, in your call to the total function on the line after your call to the sides function you're referencing variables that don't exist in you volSAtsa function. Remember that variables defined inside a function are not generally available outside that function.

Walter Roberson
Walter Roberson 2019-11-11
function volSAtsa(l , w, h)
a1 = []; a2 = []; a3 = [];
vol = volume (l, w, h);
area = sides(l, w , h);
tsa = total(a1 , a2 , a3);
output = out(vol , a1 , a2, a3 , tsa);
function vi = volume(l , w, h)
vi = l*w*h;
end
function a = sides(l , w , h)
a1 = l*w;
a2 = w*h;
a3 = l*h;
a = [];
end
function tsarea = total(a1 , a2 , a3)
tsarea = a1 + a2 + a3;
end
function results = out(vol, a1 , a2 , a3, tsa)
vi = vol; area1 = a1; area2 = a2; area3 = a3; tsarea = tsa;
fprintf(' Volume = %.2f\n ', vi)
fprintf('Area 1 = %.2f\n ', area1)
fprintf('Area 2 = %.2f\n ', area2)
fprintf('Area 3 = %.2f\n ',area3)
fprintf('Total Area = %.2f\n ' , tsarea)
end
end
This seems needlessly complex to me, but you must have had some reason for writing to a1, a2, a3 without returning them. Perhaps the assignment required you to use nested functions and shared variables ???

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by