Matlab GUI show/hide data on same plot
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Diana Becea
el 22 de Nov. de 2018
Comentada: Diana Becea
el 22 de Nov. de 2018
I am trying to make a GUI with several checkboxes. When you tick a checkbox, the data will be displayed on the graph. If you tick multiple data, all the selected data should appear. When you untick the box, only the unticked data should dissapear. I also have added sliders for each data so you can modify the color intensity for overlapping data. But it does not update in realtime, only when the box is checked and unchecked again. It would be awesome if you could give me some clues about this too :)
Now my problem is that I can either delete the whole data, or not do it at all. I have tried many things and it does not work. My full code is below. I have commented the parts that delete or not show the data in the sections. I also tried for the last 2 checkboxes the "set(plotf_bfafhp,'visible','off');" but it does not work. Do you have any sugestions as how I have to do it to make it work?
Thank you in advance! :)
function varargout = GUI_trial2(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @GUI_trial2_OpeningFcn, ...
'gui_OutputFcn', @GUI_trial2_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before GUI_trial2 is made visible.
function GUI_trial2_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% Choose default command line output for GUI_trial2
handles.output = hObject;
set(handles.slider1,'Value',1);
set(handles.slider3,'Value',1);
set(handles.slider4,'Value',1);
set(handles.slider5,'Value',1);
set(handles.slider6,'Value',1);
set(handles.slider7,'Value',1);
set(handles.slider8,'Value',1);
% Update handles structure
guidata(hObject, handles);
% --- Outputs from this function are returned to the command line.
function varargout = GUI_trial2_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
% --- Executes on button press in checkbox_b.
function checkbox_b_Callback(hObject, eventdata, handles)
if get(hObject,'Value')
raw_signal = load('dataset1.mat');
data_b = cell2mat(struct2cell(raw_signal));
axes(handles.axes1);
axis([0 3.5*10^5 -0.4 0.5]);
plott_b = plot(data_b(end,:));
x = get(handles.slider1,'Value');
plott_b.Color(4) = x;
title('Time domain');
xlabel('Time[s]');
ylabel('Voltage [V]');
hold on
grid on
% fourier
n = length(raw_signal);
t = 0:0.001:n; %time axis
L = length(t);
T = mean(diff(t)); %sampling time
Fs = 1000; %sampling frequency
Fn = Fs/2; %Nyquist frequency
Time = data_b(1,:); %time of raw signal
Good = data_b(2,:); %acual values of raw signal
Good = Good - mean(Good); % Remove D-C offset
Y = fft(Good)/L; % fourier transform normalized
Fv = linspace(0, 1, fix(L/2)+1)*Fn; % Frequency Vector
Iv = 1:length(Fv); % Index Vector For Plot
fourier = 2*abs(Y(Iv));
axes(handles.axes2);
plotf_b = plot(Fv, fourier);
plotf_b.Color(4) = x;
title('Frequency domain');
xlabel('Frequency [Hz]');
ylabel('Magnitude [dB]');
grid on
hold on
else
% cla(handles.axes1);
% cla(handles.axes2);
end
% Hint: get(hObject,'Value') returns toggle state of checkbox_b
% --- Executes on button press in checkbox_bfa.
function checkbox_bfa_Callback(hObject, eventdata, handles)
if get(hObject,'Value')
raw_signal = load('dataset2.mat');
data_bfa = cell2mat(struct2cell(raw_signal));
axes(handles.axes1);
axis([0 3.5*10^5 -0.4 0.5]);
plott_bfa = plot(data_bfa(end,:));
y = get(handles.slider3,'Value');
plott_bfa.Color(4) = y;
title('Time domain');
xlabel('Time[s]');
ylabel('Voltage [V]');
hold on
grid on
% fourier
n = length(raw_signal);
t = 0:0.001:n; %time axis
L = length(t);
T = mean(diff(t)); %sampling time
Fs = 1000; %sampling frequency
Fn = Fs/2; %Nyquist frequency
Time = data_bfa(1,:); %time of raw signal
Good = data_bfa(2,:); %acual values of raw signal
Good = Good - mean(Good); % Remove D-C offset
Y = fft(Good)/L; % fourier transform normalized
Fv = linspace(0, 1, fix(L/2)+1)*Fn; % Frequency Vector
Iv = 1:length(Fv); % Index Vector For Plot
fourier = 2*abs(Y(Iv));
axes(handles.axes2);
plotf_bfa = plot(Fv, fourier);
plotf_bfa.Color(4) = y;
title('Frequency domain');
xlabel('Frequency [Hz]');
ylabel('Magnitude [dB]');
grid on
hold on
else
% cla(handles.axes1);
% cla(handles.axes2);
end
% --- Executes on button press in checkbox_bfaaa.
function checkbox_bfaaa_Callback(hObject, eventdata, handles)
if get(hObject,'Value')
raw_signal = load('dataset3.mat');
data_bfaaa = cell2mat(struct2cell(raw_signal));
axes(handles.axes1);
axis([0 3.5*10^5 -0.4 0.5]);
plott_bfaaa = plot(data_bfaaa(end,:));
z = get(handles.slider1,'Value');
plott_bfaaa.Color(4) = z;
title('Time domain');
xlabel('Time[s]');
ylabel('Voltage [V]');
hold on
grid on
% fourier
n = length(raw_signal);
t = 0:0.001:n; %time axis
L = length(t);
T = mean(diff(t)); %sampling time
Fs = 1000; %sampling frequency
Fn = Fs/2; %Nyquist frequency
Time = data_bfaaa(1,:); %time of raw signal
Good = data_bfaaa(2,:); %acual values of raw signal
Good = Good - mean(Good); % Remove D-C offset
Y = fft(Good)/L; % fourier transform normalized
Fv = linspace(0, 1, fix(L/2)+1)*Fn; % Frequency Vector
Iv = 1:length(Fv); % Index Vector For Plot
fourier = 2*abs(Y(Iv));
axes(handles.axes2);
plotf_bfaaa = plot(Fv, fourier);
plotf_bfaaa.Color(4) = z;
title('Frequency domain');
xlabel('Frequency [Hz]');
ylabel('Magnitude [dB]');
grid on
hold on
else
% cla(handles.axes1);
% cla(handles.axes2);
end
% Hint: get(hObject,'Value') returns toggle state of checkbox_bfaaa
% --- Executes on button press in checkbox_bfaaaf.
function checkbox_bfaaaf_Callback(hObject, eventdata, handles)
if get(hObject,'Value')
raw_signal = load('dataset4.mat');
data_bfaaaf = cell2mat(struct2cell(raw_signal));
axes(handles.axes1);
axis([0 3.5*10^5 -0.4 0.5]);
plott_bfaaaf = plot(data_bfaaaf(end,:));
xx= get(handles.slider1,'Value');
plott_bfaaaf.Color(4) = xx;
title('Time domain');
xlabel('Time[s]');
ylabel('Voltage [V]');
hold on
grid on
% fourier
n = length(raw_signal);
t = 0:0.001:n; %time axis
L = length(t);
T = mean(diff(t)); %sampling time
Fs = 1000; %sampling frequency
Fn = Fs/2; %Nyquist frequency
Time = data_bfaaaf(1,:); %time of raw signal
Good = data_bfaaaf(2,:); %acual values of raw signal
Good = Good - mean(Good); % Remove D-C offset
Y = fft(Good)/L; % fourier transform normalized
Fv = linspace(0, 1, fix(L/2)+1)*Fn; % Frequency Vector
Iv = 1:length(Fv); % Index Vector For Plot
fourier = 2*abs(Y(Iv));
axes(handles.axes2);
plotf_bfaaaf = plot(Fv, fourier);
plotf_bfaaaf.Color(4) = xx;
title('Frequency domain');
xlabel('Frequency [Hz]');
ylabel('Magnitude [dB]');
grid on
hold on
else
% cla(handles.axes1);
% cla(handles.axes2);
end
% --- Executes on button press in checkbox_bfaaafhp.
function checkbox_bfaaafhp_Callback(hObject, eventdata, handles)
if get(hObject,'Value')
raw_signal = load('dataset5.mat');
data_bfaaafhp = cell2mat(struct2cell(raw_signal));
axes(handles.axes1);
axis([0 3.5*10^5 -0.4 0.5]);
plott_bfaaafhp = plot(data_bfaaafhp(end,:));
yy = get(handles.slider1,'Value');
plott_bfaaafhp.Color(4) = yy;
title('Time domain');
xlabel('Time[s]');
ylabel('Voltage [V]');
hold on
grid on
% fourier
n = length(raw_signal);
t = 0:0.001:n; %time axis
L = length(t);
T = mean(diff(t)); %sampling time
Fs = 1000; %sampling frequency
Fn = Fs/2; %Nyquist frequency
Time = data_bfaaafhp(1,:); %time of raw signal
Good = data_bfaaafhp(2,:); %acual values of raw signal
Good = Good - mean(Good); % Remove D-C offset
Y = fft(Good)/L; % fourier transform normalized
Fv = linspace(0, 1, fix(L/2)+1)*Fn; % Frequency Vector
Iv = 1:length(Fv); % Index Vector For Plot
fourier = 2*abs(Y(Iv));
axes(handles.axes2);
plotf_bfaaafhp = plot(Fv, fourier);
plotf_bfaaafhp.Color(4) = yy;
title('Frequency domain');
xlabel('Frequency [Hz]');
ylabel('Magnitude [dB]');
grid on
hold on
else
% cla(handles.axes1);
% cla(handles.axes2);
end
% --- Executes on button press in checkbox_bfafhp.
function checkbox_bfafhp_Callback(hObject, eventdata, handles)
if get(hObject,'Value')
raw_signal = load('dataset6.mat');
data_bfafhp = cell2mat(struct2cell(raw_signal));
axes(handles.axes1);
axis([0 3.5*10^5 -0.4 0.5]);
plott_bfafhp = plot(data_bfafhp(end,:));
zz = get(handles.slider1,'Value');
plott_bfafhp.Color(4) = zz;
title('Time domain');
xlabel('Time[s]');
ylabel('Voltage [V]');
hold on
grid on
% fourier
n = length(raw_signal);
t = 0:0.001:n; %time axis
L = length(t);
T = mean(diff(t)); %sampling time
Fs = 1000; %sampling frequency
Fn = Fs/2; %Nyquist frequency
Time = data_bfafhp(1,:); %time of raw signal
Good = data_bfafhp(2,:); %acual values of raw signal
Good = Good - mean(Good); % Remove D-C offset
Y = fft(Good)/L; % fourier transform normalized
Fv = linspace(0, 1, fix(L/2)+1)*Fn; % Frequency Vector
Iv = 1:length(Fv); % Index Vector For Plot
fourier = 2*abs(Y(Iv));
axes(handles.axes2);
plotf_bfafhp = plot(Fv, fourier);
plotf_bfafhp.Color(4) = zz;
title('Frequency domain');
xlabel('Frequency [Hz]');
ylabel('Magnitude [dB]');
grid on
hold on
else
set(plott_bfafhp,'visible','off');
set(plotf_bfafhp,'visible','off');
end
% --- Executes on button press in checkbox_bfaf.
function checkbox_bfaf_Callback(hObject, eventdata, handles)
if get(hObject,'Value')
raw_signal = load('dataset7.mat');
data_bfaf = cell2mat(struct2cell(raw_signal));
axes(handles.axes1);
axis([0 3.5*10^5 -0.4 0.5]);
plott_b = plot(data_bfaf(end,:));
xyz = get(handles.slider1,'Value');
plott_b.Color(4) = xyz;
title('Time domain');
xlabel('Time[s]');
ylabel('Voltage [V]');
hold on
grid on
% fourier
n = length(raw_signal);
t = 0:0.001:n; %time axis
L = length(t);
T = mean(diff(t)); %sampling time
Fs = 1000; %sampling frequency
Fn = Fs/2; %Nyquist frequency
Time = data_bfaf(1,:); %time of raw signal
Good = data_bfaf(2,:); %acual values of raw signal
Good = Good - mean(Good); % Remove D-C offset
Y = fft(Good)/L; % fourier transform normalized
Fv = linspace(0, 1, fix(L/2)+1)*Fn; % Frequency Vector
Iv = 1:length(Fv); % Index Vector For Plot
fourier = 2*abs(Y(Iv));
axes(handles.axes2);
plotf_bfaf = plot(Fv, fourier);
plotf_bfaf.Color(4) = xyz;
title('Frequency domain');
xlabel('Frequency [Hz]');
ylabel('Magnitude [dB]');
grid on
hold on
else
set(plott_bfaf,'visible','off');
set(plotf_bfaf,'visible','off');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%% SLIDERS & TEXTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
value_s1 = get(hObject,'Value');
set(handles.text2,'String',num2str(value_s1));
% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes on slider movement.
function slider3_Callback(hObject, eventdata, handles)
value_s3 = get(hObject,'Value');
set(handles.text3,'String',num2str(value_s3));
% --- Executes during object creation, after setting all properties.
function slider3_CreateFcn(hObject, eventdata, handles)
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes on slider movement.
function slider4_Callback(hObject, eventdata, handles)
value_s4 = get(hObject,'Value');
set(handles.text4,'String',num2str(value_s4));
% --- Executes during object creation, after setting all properties.
function slider4_CreateFcn(hObject, eventdata, handles)
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes on slider movement.
function slider5_Callback(hObject, eventdata, handles)
value_s5 = get(hObject,'Value');
set(handles.text5,'String',num2str(value_s5));
% --- Executes during object creation, after setting all properties.
function slider5_CreateFcn(hObject, eventdata, handles)
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes on slider movement.
function slider6_Callback(hObject, eventdata, handles)
value_s6 = get(hObject,'Value');
set(handles.text6,'String',num2str(value_s6));
% --- Executes during object creation, after setting all properties.
function slider6_CreateFcn(hObject, eventdata, handles)
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes on slider movement.
function slider7_Callback(hObject, eventdata, handles)
value_s7 = get(hObject,'Value');
set(handles.text7,'String',num2str(value_s7));
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
% --- Executes during object creation, after setting all properties.
function slider7_CreateFcn(hObject, eventdata, handles)
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes on slider movement.
function slider8_Callback(hObject, eventdata, handles)
value_s8 = get(hObject,'Value');
set(handles.text8,'String',num2str(value_s8));
% --- Executes during object creation, after setting all properties.
function slider8_CreateFcn(hObject, eventdata, handles)
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
2 comentarios
Jan
el 22 de Nov. de 2018
Editada: Jan
el 22 de Nov. de 2018
I also tried for the last 2 checkboxes the "set(plotf_bfafhp,'visible','off');" but it does not work
"It does not work" is not enough to understand, what the problem is. Do you get an error message?
By the way: In your case it does not matter the total run-time, but in general it is better to use teh cheap constant 3.5e5 instead of the expensive power operation and mutliplication 3.5*10^5.
Respuesta aceptada
Jan
el 22 de Nov. de 2018
I guess, that you forgot to share the handles between the callbacks.
function checkbox_bfafhp_Callback(hObject, eventdata, handles
...
handles.plotf_bfafhp = plot(Fv, fourier); % Update handles struct
...
guidata(hObject, handles); % Store updated handles struct in the figure
end
Then there is no reason to replot the line, if it is invisible only:
function checkbox_bfafhp_Callback(hObject, eventdata, handles
...
if get(hObject,'Value')
if isfield(handles, 'plotf_bfafhp')
set(handles.plotf_bfafhp, 'Visible', 'on');
else
... create the plot
handles.plotf_bfafhp = plot(Fv, fourier); % Update handles struct
end
end
...
guidata(hObject, handles); % Store updated handles struct in the figure
end
Más respuestas (0)
Ver también
Categorías
Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!