I am getting an empty figure for a given code

Hello, I have been given the following code and the data as well. However when i run it, the figure is empty. I cant find the problem and there is no error. (maybe the problem is with the findpeaks function)
Can someone help me?
Thank you !!
load modulation_125911_0dBAtt_1kOhm_100kOhmPort6.mat
set(0, 'DefaultAxesFontSize',14)
set(0, 'defaultLineLineWidth', 1.5)
R = data{1}+1i*data{2};
Rs = squeeze(R);
phi = scan.loops(1).rng;
phi = linspace(phi(1), phi(2), scan.loops(1).npoints);
freq = scan.loops(2).rng;
freq = linspace(freq(1), freq(2), scan.loops(2).npoints);
ref1x = [1, 402];
ref2x = [1, 402]; % just so i dont get an error... should be adjusted when i see the plot!
w = .5*tanh((freq-3e9)/5e7)+.5;
phase = exp(1i*(freq-freq(1))*-7e-10);
ref = w.*mean(data{1}(:, ref2x(1):ref2x(2))') ...
+ (1-w).*mean(data{1}(:, ref1x(1):ref1x(2))') ...
+1i * ( w.*mean(data{2}(:, ref2x(1):ref2x(2))') ...
+ (1-w).*mean(data{2}(:, ref1x(1):ref1x(2))'));
offset = 0;
R = data{1}+1i*data{2};
Rcorr = (R.*repmat(phase, length(phi), 1)' - offset) ...
./repmat(ref.*phase - offset, length(phi), 1)';
% Load flux/frequency data
phi = scan.loops(1).rng;
phi = linspace(phi(1), phi(2), scan.loops(1).npoints);
ph = phi(1:end-1);
freq = scan.loops(2).rng;
freq = linspace(freq(1), freq(2), scan.loops(2).npoints)./1e9;
% Calculate crude estimate for mA to Phi_0 conversion
%[~, ind] = max(diff(Rs, 1, 2));%
%[~, ind] = min(Rcorr);%
%osc = freq(ind);
%[~, ind] = min(osc(lB:uB));
Rdiff = diff(Rs,1,2);
osc = [];
phi_red = [];
for i=1:length(phi)-1
[a, locs] = findpeaks(abs(Rdiff(:, i)), 'MinPeakProminence', 0.07);
if(~isempty(locs))
if(length(locs) > 1)
[~, locs] = max(abs(Rdiff(locs, i)));
end
% cut away a lower frequecies
if(freq(locs) > 5.8)
osc = vertcat(osc, freq(locs));
phi_red = vertcat(phi_red, phi(i));
end
end
end
osc = smooth(osc)';
% osc = vertcat(osc(50:160), osc(597:706));
figure(1);
plot(phi_red, osc,'.');
drawnow;

 Accepted Answer

Hello,
Your variable phi_red is empty. Because your variable loc is empty, it does not run the if(~isempty(locs) command and replace your variable that you previously defined as phi_red = [ ];
The problem seems to be the specification 'MinPeakProminence', 0.07, with this, no 'relevant' peaks are found.

11 Comments

To expand on this answer: the largest prominence that is found is 0.00220830. If you lower that value you will see a plot.
Hello Mara,
Thank you for your answer. I tied to change the threshold of 0.07 to different values(smaller and bigger)... but no image still.
Is there a way around this? (I'm relatively new to Matlab)
If you leave away 'MinPeakProminence', 0.07 completely, or , as Rik suggested lower the threshold to 0.002 or lower, you will get one!
But if this helps you depends on what you want to find out in your dataset, too.
As your threshold was much higher than the highest peak in your data, I suppose that data values like yours were not expected by who wrote the script. So I suggest, even if you get a plot, you should check if everything is right with your values!
Hello Rik,
Thank you for your help. However, I am still not getting anything :/ !!! I also tried to leave away this section as Mara suggested, but also nothing...
Hey Mara,
you're totally right !! The thing is I was hesitant to change it since the script as well as the data are part of an experimental course. I also have plenty of other "modulation" data like the one i uploaded; but not a single one is working with this script. I will check everything again but it's been nearly 4 days that I am stuck on this part
Thank you for everything !!
I got a plot, but I also left away these commands, since I do not have the necessary toolbox
% osc = smooth(osc)';
% osc = vertcat(osc(50:160), osc(597:706));
No worries, good luck!
Hey, i closed matlab and re-opened it again, and followed your suggestions, now it works !!
Thanks to both of you Mara and Rik.
Mara
Mara on 20 Jun 2020
Edited: Mara on 20 Jun 2020
That's good to hear! No problem!
(Sometimes it is good to have clear; at the start of the code so no old variables are in the workspace, maybe that was the problem)
It is better to work with functions, which will automatically keep your workspace clear, and will even warn you about variables you're not using (which might point you to a typo).
Deal, I will make sure to define it as a function then!!
Many thanks!!

Sign in to comment.

More Answers (0)

Products

Release

R2019a

Asked:

on 20 Jun 2020

Commented:

on 21 Jun 2020

Community Treasure Hunt

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

Start Hunting!