Array value changing for no apparent reason?? Is it possible there is hidden lines of code?

In the attached photo, you can see the displayed value in the command window changes between the two breakpoints but the only code executed is a display, this shouldn't change the value, whats happening??

3 Comments

I'd guess sweepData is global and updated from an instrument in background...
ah so even though there is no code here changing anything, if the source of the 'sweepData' data is updated, it will update it anyway? I was guessing this, but it always changes at this point in the loop and not anywhere else where sweepData is used
The problem is that noted in the Answer, but that doesn't reflect the image -- if the coder were identical to and only what this image shows the change would show up between the first two disp calls. Of course, you can't see the timing between output and the initiation so in fact where it does change will be in between those two lines in actuality, you just can't correlate the two from the image alone.

Sign in to comment.

 Accepted Answer

But you've got
sweepData(sweepData<=threshold) = threshold;
in here that will change sweepData if there's any value in there that satisfies the condition of less than; only if there are values that are precisely equal to the threshold already would they be unchanged.
---> *here*
display(sum(sweepData))%%%%%%%%%%%%%%%%%%%%%%
---> *to here*
display(sum(staticSweepData))%%%%%%%%%%%%%%%%%%%%%%
Then you display the previously saved data prior to making the above change and display it in comparison to the new set.
Do this at a breakpoint before and after the above test --
sum(sweepData==threshold)
and it'll tell you how many were substituted.
You could also search for the locations that satisfy the condition and compare their initial value to the threshold, sum that difference and reproduce the difference between the first and second sums...

3 Comments

Bingo! I was using this line just for plotting reasons, I didn't think it changed the actual array but instead just plot values under the threshold to the threshold making it appear as a mask to ignore signals not of interest. Just to show you what I mean I have attached a picture of the output graphs. The main problem I am having is that at some point the sweepData does not update and so the percentage of on time of each frequency of the band tends toward either 100% or 0% depending on whether they are over or under the threshold. Sometimes it works for 10000 sweeps, sometimes for 100. I will try use what you guys have given me to figure it out
Thanks again for the help
Again an easy solution for a problem which looked like pure magic. +1
"I didn't think it changed the actual array..."
It's "logical addressing", one of the most powerful features of Matlab. It's no different than writing
x(1:3)=threshold;
as far as result which clearly is an assignment; the only difference is the index vector is a vector of logical and the locations in the vector that are true by position are those that are addressed; the false locations are left unchanged.
It doesn't appear the problem regarding the symptoms regarding magnitudes has anything to do with this; that has to be something in the collection process that we have no data for.
Good luck...at least there isn't something truly mysterious going on with this particular issue! :)

Sign in to comment.

More Answers (1)

As far as I can see, the only command, which is executed during the value changes is not display(), but title. Pleas be so kind and post exactly, in which line the value changes. Then you can be sure, that it is either this command, or if the line if not reproducible, it can be a timer or GUI callback. But then sweepData must either be a function, global variable or class. If you explain, what sweepData is and where the change happens, writing an answer would require less guessing.
Of course there can be hidden lines: Scroll to the right. Perhaps some code appears on the right outside the visible area of the editor. Therefore it would be much better to post the code by copy&paste instead of a screenshot, because then the "hidden" code would appear automatically.

7 Comments

The value changes from line 59 to 60 after the display. The second last value -354480 is displayed after continuing from the breakpoint on line 55, then when i continue from line 59 the value changes to -3.48650. I scrolled off screen there is nothing there. I assumed there was something happening in the background so i put in all of these displays to see where it changes, 'sweepData' is in many lines before the point where the value changes. I set it equal to 'staticSweepData' which seems to keep the value from the start of the loop, but very curious as to why this happens.
cheers for the reply! hope this is a little less vague
for ii= 1:numCapture
[sweepData,startFreqMHz,stopFreqMHz,numSamples,freqs] = step(sweep_cap);
display(sum(sweepData))%%%%%%%%%%%%%%%%%%%%%%
staticSweepData = sweepData;
display(sum(sweepData))%%%%%%%%%%%%%%%%%%%%%%
if isequal(sweepData, sweepDatacompare)
% reset(sweep_cap);
[sweepData,startFreqMHz,stopFreqMHz,numSamples,freqs] = step(sweep_cap);
display(sum(sweepData))
end
display(sum(sweepData))%%%%%%%%%%%%%%%%%%%%%%
display(sum(staticSweepData))%%%%%%%%%%%%%%%%%%%%%%
if ii == 1
c = zeros(1,numSamples);
percent_time_on = zeros(1,numSamples);
end
display(sum(sweepData))%%%%%%%%%%%%%%%%%%%%%%
display(sum(staticSweepData))%%%%%%%%%%%%%%%%%%%%%%
%mean = (sum(findpeaks(sweepData))/length(findpeaks(sweepData)));
%threshold = -70;
mean = sum(sweepData)/length(sweepData);
threshold = mean;
display(sum(sweepData))%%%%%%%%%%%%%%%%%%%%%%
display(sum(staticSweepData))%%%%%%%%%%%%%%%%%%%%%%
sweepData(sweepData<=threshold) = threshold;
xlim([startFreqMHz stopFreqMHz])
---> *here*
display(sum(sweepData))%%%%%%%%%%%%%%%%%%%%%%
---> *to here*
display(sum(staticSweepData))%%%%%%%%%%%%%%%%%%%%%%
display(sum(sweepData))%%%%%%%%%%%%%%%%%%%%%%
display(sum(staticSweepData))%%%%%%%%%%%%%%%%%%%%%%
%subplot(2,1,1)
% plot(freqs(1:numSamples),sweepData(1:numSamples),'k');
display(sum(sweepData))%%%%%%%%%%%%%%%%%%%%%%
display(sum(staticSweepData))%%%%%%%%%%%%%%%%%%%%%%
plot(freqs(1:numSamples),sweepData(1:numSamples),'k');
display(sum(sweepData))%%%%%%%%%%%%%%%%%%%%%%
display(sum(staticSweepData))%%%%%%%%%%%%%%%%%%%%%%
The most important detail is still concealed: What is the type of sweepData? The step functions seems to be not the one from the Matlab toolbox: https://www.mathworks.com/help/control/ref/step.html, or is it? There is no magic remote changing of variables, except that this has been programmed explicitely. So is this a global variable or a class? Do you use any code for creating variables remotely like evalin or assignin and run it during debugging? Does the change happen reproducible at the some code even if you are not debugging?
You screenshot contains this at line 59 and 60:
display(sum(sweepData))%%%%%%%%%%%%%%%%%%%%%%
display(sum(sweepData))%%%%%%%%%%%%%%%%%%%%%%
The posted code the lines:
display(sum(sweepData))%%%%%%%%%%%%%%%%%%%%%%
display(sum(staticSweepData))
Perhaps you have created a P-file and run an older Matlab version? Then stepping through the code shows the contents of the M-file in the editor, but the P-file is evaluated. This can look very strange and I'm happy that this debugging of P-files has been disabled in modern Matlab versions. Use which FUNCNAME -all to check this, replacing FUNCNAME by the name of the function file.
This program is based off sample code that goes along with the CRFS matlab toolbox only recently released and unfortunately I don't believe I can share too much code, better safe than sorry.
In the main m file, the main loop takes data from a specified band of the spectrum like power and frequency, the sweepData gives power level. It seemed to me that at the start of the loop it take the data from one sweep using:
[sweepData,startFreqMHz,stopFreqMHz,numSamples,freqs] = step(sweep_cap);
..but should not update the vales unless this line is used again
As far as type, and I apologise for incorrect erminology, but it appears to be an object/function of a class If this is not sufficient then I will delete the question and research functions and global variables, at least I have some key words to search!
You can check the class: Stop in the code by a breakpoint. Then type:
class sweepData
isglobal sweepData
which sweepData -all
An alternative idea is a strange bug: Does the toolbox contain compiled C-code (so called mex files)? You can access memory in C without having the permission to do so. Then the value of a variable can change randomly. But then the problem is not reproducible, and most likely Matlab would crash comppletely.
K>> class sweepData
ans =
'char'
K>> which sweepData -all
sweepData is a variable.
i have found why the value changes, my problem still remains though which i thought may have been caused by this change, but these tips are still useful
many thanks
Okay so the real problem hear is that the instrument I'm measuring from sometimes stops working and the data is not updated correctly, and the percentage of 'on' time of each frequency of the band tends toward either 100% or 0% depending on whether they are over or under the threshold. When this error begins, I pause and check the sum of the sweepData, but it says undefined function or variable, so the whole thing is screwing up and I have no idea why! Such is life
Sorry, a typo:
class(sweepData) % not: class sweepData
But the problem is solved already. Did you see, that I've mentioned the inconsistency between "sweepData" and "staticSweepData" 2 hours ago already?

Sign in to comment.

Categories

Find more on Scripts in Help Center and File Exchange

Asked:

on 30 Jun 2017

Edited:

dpb
on 1 Jul 2017

Community Treasure Hunt

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

Start Hunting!