Find the value which causes an error from a vector
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?
Answers (1)
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
Christian Berwanger
on 18 Nov 2020
The result of this function is an array of the same size like the input - i think the problem is not inside this function or its output. What kind of software do you use?
- The way you use isempty can not work. isempty gives a logic 1 only on empty arrays.
- You want to select values depending on the value of T, but then you make operations on the whole svol vector - this will not work.
The best way is to use the debugger to learn what goes wrong:



Star Strider
on 18 Nov 2020
Typographical error:
svol(T>=T_grenz) = 50*T(T>T_grenz);
↑ ← HERE
Should be:
svol(T>=T_grenz) = 50*T(T>=T_grenz);
.
Christian Berwanger
on 18 Nov 2020
Edited: Christian Berwanger
on 18 Nov 2020
Christian Berwanger
on 18 Nov 2020
Categories
Find more on MATLAB 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!