How can I make and save a log file of calculated mean squared value from raw EMG when pushing button?

1 view (last 30 days)
Hello, I and my teammates already wrote the code which calculates and shows mean squared value using recent 10 data points of EMG in real time. When the button is pushed, EMG data from 8 channels starts to be streamed and gathered and MSV also starts to be calculated and shown in real time.
I'd like to make and save a log for these calculated MSV values from the 8 channels which change over time. I've wrote some codes which allocates the new variable for saving MSV values. I've also tried the code by writing writematrix function to save the data as Excel file, but I failed to save. Are there any tips or advices for it?
This is the part of the code I've wrote.
msvlog = [0,0,0,0,0,0,0,0]; %Making New variable for saving calculated MSV from 8 channels
% Create a push button
btn = uibutton(fig,'state',...
'Text','Streaming', ...
'Position',[10, 700, 100, 30],...
'ValueChangedFcn', @(btn,event) plotButtonPushed(btn, ax, myo, charac_val, msvlog, parameter));
Unrecognized function or variable 'fig'.
% 'ax' is for showing each channel of EMG
function plotButtonPushed(btn, ax, myo, charac_val, msvlog, parameter)
if btn.Value == true
myo.startStreaming; % Start Streaming_start record the emg
for k = 1:6000
emg = myo.emg_log; %myo.emg_log = function that starts to make log file of emg
if numel(emg) ~= 0 && btn.Value == true
x = 1:length(emg);
for j = 1:8 %~106 line
msv = 0;
y = emg(:,j);
ax(j).XLim = [0 length(emg)/200];
ax(j).YLim = [-1 1];
if length(y) > 10
for l = 0:9
msv = msv + y(length(y)-l)^2; %calculate msv using recent 10 dataset
end
msv = msv / 10;
else
for l = 0:length(y)-1
msv = msv + y(length(y)-l)^2;
end
msv = msv / length(y);
end
charac_val(j).Text = num2str(msv);
plot(ax(j),x/200,y)
msvlog(k,j) = msv;
end
elseif btn.Value == false
break;
end
drawnow; %Draw the real-time plot for raw EMG
pause(0.02)
end
else
if myo.isStreaming == true
myo.stopStreaming;
writematrix(msvlog, parameter)
end %If streaming is on, making it off
end
end

Answers (1)

Tushar
Tushar on 12 Sep 2023
Hi Jiwon,
I understand that you are looking to save 'log files' from your MATLAB code. To achieve this, you can utilize the 'diary' command, which allows you to log everything on the command window to a file. Here is how you can use it:
  • Ensure logging is turned on by using the following commands:
diary on; %turn logging on
get(0,'Diary'); %returns whether logging is 'on' or 'off'
  • After every update of the calculated MSV values, display them as an output to the command window.
  • To log the outputs to a file of your choice, use the following command:
diary myLogFile; %enable logging and save to myLogFile
For more information about 'log files' and how to use the 'diary' command, you can refer to the below documentation:
Furthermore, please ensure that the 'parameter' in the line below corresponds to a valid filename with the correct extension. For Excel spreadsheet files, make sure to use file extensions such as .xls, .xlsm, or .xlsx
writematrix(msvlog, parameter);
For more information on the 'writematrix' function and writing to Excel files, you can refer to the below documentation:
I hope this helps,
Tushar Agarwal

Categories

Find more on Data Import from MATLAB in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!