String Comparison giving weird results, false for a true case.

2 views (last 30 days)
Hi, I am writing code to take data from a large matrix(Measurements) and to break it into arrays of its subcomponents, for example Temperature. For the most part its fully functional besides in the cases where the component of the character array measurements, has empty or missing values. To try find out why it was not working i added lines 3 and 4 so that i could compare them.
for j = 2:1:289
Measurements(j,i)
string(missing)
strcmp(Measurements(j,i),string(missing))
if strcmp(Measurements(j,i),string(missing)) == 0
Temperature(j-1) = str2double(Measurements(j,i));
elseif strcmp(Measuremnts(j,i),string(missing)) ==1
break
end
end
_________________________________________________________________________________________________________________________________
ans =
"20.494556"
ans =
<missing>
ans =
logical
0
ans =
<missing>
ans =
<missing>
ans =
logical
0
__________________________________________________________________________________________________________________________________________
so those are a set of outputs, for some reason the code is giving a positive response even when the strings are the same, in which case it should break out of the loop but instead it records the missing values in the arrays as well.
How can I fix this? Why is it giving a are both cases in the string comparison given as , when they are clearly different?
Thanks.
  1 Comment
Geoff Hayes
Geoff Hayes on 3 Apr 2020
Joachim - what can you say about the data type for the measurement? It is clear that
string(missing)
is a string array (but I thought that you would have needed to add single quotes around 'missing' ??) but how about Measurements(j,i)? What does
class(Measurements(j,i))
return? Is it a string array or a character array? Maybe it doesn't matter (my version is so old that I don't have the string function...)

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 3 Apr 2020
If you're looking to find and/or fill missing data take a look at the ismissing and fillmissing functions.
rng default
A = randn(10, 1);
A(abs(A) > 0.75) = missing;
B = fillmissing(A, 'previous');
C = fillmissing(A, 'next');
D = fillmissing(A, 'linear');
miss = ismissing(A)
results = table(A, miss, B, C, D) % for ease of comparing the fill methods

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!