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

18 views (last 30 days)
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

Ayush Singh
Ayush Singh on 4 Mar 2024
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
Connor
Connor on 4 Mar 2024
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

Community Treasure Hunt

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

Start Hunting!