How do I create a GUI that runs specific sections of code?

3 views (last 30 days)
Mattis Roost
Mattis Roost on 10 Aug 2021
Edited: Mattis Roost on 10 Aug 2021
Hello everyone,
I am fairly new to Matlab and I have a problem creating a GUI using App Designer. I have the following piece of code that establishes a connection to my PicoScope 2204A, generates a sine wave, measures this wave in Channel A and then plots it. It works perfectly fine:
%% ESTABLISH CONNECTION AND GENERATE SIGNAL
clear;
clc;
close all;
%% Load Configuration Information
PS2000Config;
%% Device Connection
% Create a device object.
ps2000DeviceObj = icdevice('picotech_ps2000_generic.mdd');
% Connect device object to hardware.
connect(ps2000DeviceObj);
%% Obtain Signal Generator Group Object
% Signal Generator properties and functions are located in the Instrument
% Driver's Signalgenerator group.
sigGenGroupObj = get(ps2000DeviceObj, 'Signalgenerator');
sigGenGroupObj = sigGenGroupObj(1);
%% Built-in Signal Generator
% Output a sine wave.
%
% Default offset (0mV) and peak to peak voltage (2000mV) values are used.
% Set the startFrequency to output a waveform with a constant frequency of
% 500Hz.
set(sigGenGroupObj, 'peakToPeakVoltage', 1000);
set(sigGenGroupObj, 'startFrequency', 1000);
% Wave type : ps2000Enuminfo.enPS2000WaveType.PS2000_SINE
[status.sigGenSimple] = invoke(sigGenGroupObj, 'setSigGenBuiltInSimple', 0);
%% MEASURE SIGNAL AND PLOT IT
% Obtain references to device groups to access their respective properties
% and functions.
% Block specific properties and functions are located in the Instrument
% Driver's Block group.
blockGroupObj = get(ps2000DeviceObj, 'Block');
blockGroupObj = blockGroupObj(1);
%% Device Configuration
% Set channels:
% Channel : 0 (ps2000Enuminfo.enPS2000Channel.PS2000_CHANNEL_A)
% Enabled : 1 (PicoConstants.TRUE)
% DC : 1 (DC Coupling)
% Range : 6 (ps2000Enuminfo.enPS2000Range.PS2000_1V)
[status.setChA] = invoke(ps2000DeviceObj, 'ps2000SetChannel', 0, 1, 1, 6);
% Channel : 1 (ps2000Enuminfo.enPS2000Channel.PS2000_CHANNEL_B)
% Enabled : 1 (PicoConstants.TRUE)
% DC : 1 (DC Coupling)
% Range : 7 (ps2000Enuminfo.enPS2000Range.PS2000_2V)
[status.setChB] = invoke(ps2000DeviceObj, 'ps2000SetChannel', 1, 0, 1, 7);
%% Data Collection
% Capture a block of data on Channels A and B together with times. Data for
% channels is returned in millivolts.
disp('Collecting block of data...');
% Execute device object function(s).
[samplingIntervalUs, maxBlockSamples] = invoke(blockGroupObj, 'setBlockIntervalUs', 20);
[bufferTimes, bufferChA, bufferChB, numDataValues, timeIndisposedMs] = invoke(blockGroupObj, 'getBlockData');
disp('Data collection complete.');
%% Stop the Device
% Additional blocks can be captured prior to stopping the device.
stopStatus = invoke(ps2000DeviceObj, 'ps2000Stop');
%% Process Data
% Process data as required. In this example the data is displayed in a
% figure.
disp('Plotting data...')
% Find the time units used by the driver.
timesUnits = timeunits(get(blockGroupObj, 'timeUnits'));
% Append to string.
timeLabel = strcat('Time (', timesUnits, ')');
% Plot the data.
figure1 = figure('Name','PicoScope 2000 Series Example - Block Mode Capture', ...
'NumberTitle', 'off');
plot(bufferTimes, bufferChA);
title('Block Data Acquisition');
xlabel(timeLabel);
ylabel('Voltage (mv)');
legend('Channel A');
grid on;
%% STOP SIGNAL GENERATOR AND DISCONNECT DEVICE
% Turn Off Signal Generator
% Turn off the signal generator (set the output to 0mV DC).
set(sigGenGroupObj, 'offsetVoltage', 0);
set(sigGenGroupObj, 'peakToPeakVoltage', 0);
[sigGenOffStatus] = invoke(sigGenGroupObj, 'setSigGenOff');
%% Disconnect
% Disconnect device object from hardware.
disconnect(ps2000DeviceObj);
delete(ps2000DeviceObj);
I now want to creat a GUI that goes through this code "step-by-step" with the possibility to take more than one measurement. I want it to have a "start" button that establishes the connection to the PicoScope and generates the signal, a "measurement" button that measures the signal and plots it and a "stop" button that stops the signal generator and disconnects the PicoScope. When I try to just seperate the code into three callback functions, I encounter the following problem:
The "start" button works perfectly. But when I press the "measurement" button, my app seems to have forgotten which oscilloscope I am talking about.
I think I somehow have to save the configuration data I get when executing
PS2000Config;
in properties, but I can not figure out how to do it the right way. If anyone of you knows the answer to my struggle I would really appreciate your help.
Cheers,
Mattis
  2 Comments
Mattis Roost
Mattis Roost on 10 Aug 2021
Thank you very much! I will try to implement your advice.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by