I think, I might have a problem with the 'hold' part and I get the 'error while evaluating button privatebuttonpushedfcn'

7 views (last 30 days)
i have this in the code view for the app:
% Button pushed function: PlotButton
function PlotButtonPushed(app, event)
fnam = uigetfile ('*.dat','Escolha o ficheiro');
fid = fopen(fnam);
ecg = fscanf(fid,'%f');
fs = 200; %sampling rate
sze = length(ecg);
necg = ecg/max(ecg); % normalize the maximum value to unity
time = (1:sze)/fs;
[mins, maxs] = picos (necg);
figure;
plot(app.UIAxes, time, necg, 'b');
hold on;
plot (app.UIAxes, time(maxs), necg(maxs), 'red*');
plot (app.UIAxes, time(mins), necg(mins), 'ko'); hold off;
axis tight;
ylabel(app.UIAxes, 'ECG');
xlabel(app.UIAxes, 'Time in seconds');
But when i press run, and then the plot button, choose a file, all i get in the Axes window is the black dots that mark valleys in the signal plus, it opens a different window with a blank figure. I need to show the signal, the valleys marked with a dot and the peaks marked with a star in the app.UIAxes and i dont want to open the other window with the blank figure.
Here is the code for "picos" function:
function [mins, maxs] = picos (sinal)
mins = [];
maxs = [];
t = 1:length (sinal);
for i = 2:length(sinal)-1
if sinal(i-1) > sinal(i) && sinal(i) < sinal(i+1)
mins = [mins i]; %faz acrescentar o valor de i ao array mins
end
if sinal(i-1) < sinal(i) && sinal(i) > sinal(i+1)
maxs = [maxs i]; %faz acrescentar o valor de i ao array maxs
end
end
% plot (t, sinal, 'm-'); hold on;
% plot (t(maxs), sinal(maxs), 'b*');
% plot (t(mins), sinal(mins), 'go'); hold off;
% title ('Deteção picos'); xlabel ('Tempo (s)'); ylabel ('Sinal (t)');
end
Thank you!

Accepted Answer

Voss
Voss on 2 Oct 2024

You need to tell hold() and axis() to operate on your app's axes:

...
hold(app.UIAxes,'on')
...
hold(app.UIAxes,'off')
...
axis(app.UIAxes,'tight')
...

And remove the "figure;" call if you don't want a blank figure to pop up.

More Answers (1)

Steven Lord
Steven Lord on 2 Oct 2024
Don't call figure if you don't want to create a new figure.

Categories

Find more on App Building 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!