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.
0 个评论
回答(3 个)
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
0 个评论
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.
0 个评论
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 ???
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!