Find the value which causes an error from a vector
1 view (last 30 days)
Show older comments
Hello,
I do have the following code:
function [test] = func(T)
T_grenz=112;
if T>=T_grenz
svol = 50*T;
elseif T<T_grenz
svol = 100*T;
end
if svol==0
svol = 0.001;
elseif isemtpy(svol)
error("svol is empty at T="); %I want to add this error message correctly
end
test=1/svol;
This matlab script is used by a simulation software. The software calls the matlab function with like a 1x10000 vector and expects a 1x10000 vector as return value.
While running this for some reason matlab gets the error that svol is not defined. However I think in every case svol should have a value. Now I want to reproduce the error. How can I get exact values of my vector T to reproduce the bug. I do not want the whole vector. Just the one value that cause the error. Can I do this with vectors? Or do I have to write the for loop manually to iterate through the vector?
0 Comments
Answers (1)
Stephan
on 18 Nov 2020
Edited: Stephan
on 18 Nov 2020
This works with logical indexing, therefore avoids the if / else logic and should ensure that you do not need any error messages:
function test = func(T)
T_grenz=112;
[r,c] = size(T);
svol = zeros(r,c);
svol(T>=T_grenz) = 50*T(T>T_grenz);
svol(T<T_grenz) = 100*T(T<T_grenz);
svol(svol==0) = 0.001;
test=1./svol;
end
5 Comments
See Also
Categories
Find more on Testing Frameworks 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!