how to write arrays directly to the base workspace from the app designer based on data collected using the data acquisition toolbox?

I have been having trouble collecting data then saving using the live data acquistion example. Since I have tried to use the assignin & evalin functions to write variables directly into the base workspace but I am having difficulties accomplishing this. I saw some things about simulink but they did not make much sense to me. Does anyone know what I might need to do? I know I might need to make the variables public but I'm not to sure where to start.
All I am trying to write is one variables that contains timestamps and multiple channels worth of data collected using the data acqusition toolbox.
Thanks!

 Accepted Answer

Hi Connor
To manage the task of collecting data and then saving it, especially when using the Data Acquisition Toolbox in MATLAB, you can focus on MATLAB's capabilities which are suffice for most data acquisition and logging needs. The process involves setting up your data acquisition session, collecting data, and then saving this data to the MATLAB base workspace or directly to a file.
Here's a simplified approach to handle this:
1.Set Up Data Acquisition
First, ensure you have the necessary hardware support package installed for your data acquisition device. Then, you can set up your data acquisition session. For example, using a National Instruments device:
d = daq.createSession('ni');
addAnalogInputChannel(d, 'Dev1', 0, 'Voltage');
2. Collect Data
To collect data, you can use the `startForeground` function for synchronous operations or `startBackground` for asynchronous operations. Here's an example of collecting data synchronously:
[data, timestamps] = startForeground(d);
This command will block MATLAB execution until all data is collected. `data` will contain the collected data, and `timestamps` will contain the corresponding timestamps.
3. Saving Data to Base Workspace
If you want to save the `data` and `timestamps` directly to the MATLAB base workspace, you can use the `assignin` function:
assignin('base', 'collectedData', data);
assignin('base', 'collectedTimestamps', timestamps);
This will create variables `collectedData` and `collectedTimestamps` in the base workspace.
4. Making Variables Accessible
Since you mentioned making variables public, if you're working within a function or script and you want these variables to be accessible from the base workspace or other functions, using `assignin` as shown above is the correct approach.
Hope it helps!

4 Comments

I am already using all the steps here. The problem I am having is that assignin wont work in app designer and I am getting errors.
When working with App Designer or other GUI tools in MATLAB, you indeed cannot use `assignin` in the traditional sense to directly manipulate the base workspace.. However, there are several strategies you can employ to achieve similar outcomes, such as sharing data between your App (or GUI) and the MATLAB workspace.
1. Using App Properties for Data Storage
One common approach is to use public properties within the App to store the data. These properties can then be accessed from the base workspace if you have a handle to the App instance.
Here is a simplified example:
In your App Designer Code:
classdef myApp < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
CollectDataButton matlab.ui.control.Button
end
properties (Access = public)
Data % This will store your timestamps and channels data
end
methods (Access = private)
% Callback function for the button
function collectDataButtonPushed(app, event)
% Example data collection process
app.Data = rand(10, 5); % Assume this is your collected data
disp('Data collected');
end
end
% App initialization and construction
methods (Access = public)
% Constructor
function app = myApp
% Create UIFigure and components
createComponents(app)
% More code here to set up your app...
end
% Create UIFigure and components
function createComponents(app)
% Create UIFigure
app.UIFigure = uifigure;
app.UIFigure.Position = [100 100 400 300];
% Create CollectDataButton
app.CollectDataButton = uibutton(app.UIFigure, 'push');
app.CollectDataButton.ButtonPushedFcn = createCallbackFcn(app, @collectDataButtonPushed, true);
app.CollectDataButton.Position = [150 130 100 22];
app.CollectDataButton.Text = 'Collect Data';
end
end
end
Accessing Data from the Workspace:
After you run your App, you can access its data from the MATLAB Command Window like so:
% Assuming you've started your app and its variable name is 'appInstance'
dataFromApp = appInstance.Data;
Make sure to replace `appInstance` with the actual variable name of your app instance.
2. Callback Functions to External Scripts or Functions
Another approach is to use callback functions that call external scripts or functions. These scripts or functions can then use the `assignin` function to write data to the base workspace.
3. Using `guidata` or SET/GET with Handles (For GUIDE or Legacy GUIs)
If you are using GUIDE or custom GUIs built with legacy functions, `guidata` or the `set`/`get` functions with figure handles can be used to store and retrieve data. This approach is less relevant for App Designer apps.
Hope it helps!
I was able to use assignin, I think I may have been using it wrong or I changed it from a private to public property. I was wondering if you might know why it is writing out more channels than it should be?
The following is the code that I used for the Assignin and the data it is based off of.
this is in my callback function for my UItable which selects the channel that they are collecting in
app.indices = event.Indices;
I also have the variable that I use for my liveaxes and the assignin function that I use
app.DataFIFOBufferch1 = storeDataInFIFO(app, app.DataFIFOBufferch1, buffersize, app.calibratedData(:, app.indices));
%this gave me the 4 channel image you see below
app.DataFIFOBufferch1 = storeDataInFIFO(app, app.DataFIFOBufferch1, buffersize, app.calibratedData);
%this gave me the 8 channel imgage you see below
I only selected 2 channels in my UI table but either got replicas of other channels or it showed all my channels.
the assignin function that I use
assignin("base","data",app.DataFIFOBufferch1)
This is the function that I use to calibrate my data.
function calibratedData = calibrateData(~,data, slopes, intercepts, indices)
% Calibrate the data using slopes and intercepts
calibratedData = data;
for i = 1:numel(indices)
calibratedData(:, i) = calibratedData(:, i) * slopes(:, i) + intercepts(:, i);
end
I tried to change a coulple values and it just ended up showing all the channels instead.
Thanks.

Sign in to comment.

More Answers (0)

Products

Release

R2023b

Asked:

on 1 Mar 2024

Commented:

on 5 Mar 2024

Community Treasure Hunt

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

Start Hunting!