How can I import data from .csv file with numeric values and texts (with column headers) into MATLAB Workspace?
0 Kommentare
Antworten (7)
0 Kommentare
0 Kommentare
0 Kommentare
1 Kommentar
1 Kommentar
2 Kommentare
function HandGesture_GUI_3ButtonsRow_ExpandedPred_MotorsSelective %% GUI Setup fig = figure('Name','Hand Gesture EMG GUI','Position',[100 100 950 550]);
% Hand plot axes (يمين) axHand = axes('Parent',fig,'Position',[0.45 0.55 0.5 0.35]); axis(axHand,[0 10 0 10]); axis(axHand,'equal'); axis(axHand,'off'); axis(axHand,'manual'); title(axHand,'Hand Gesture');
% EMG plot axes (يمين تحت اليد) axEMG = axes('Parent',fig,'Position',[0.45 0.15 0.5 0.35]); xlabel(axEMG,'Time (s)'); ylabel(axEMG,'Amplitude'); title(axEMG,'EMG Signal'); grid(axEMG,'on'); axis(axEMG,'manual'); xlim(axEMG,[0 1]); ylim(axEMG,[-2 2]);
% Gestures gestures = {'Fist','Open','Point','Peace','ThumbsUp'}; nGestures = length(gestures);
% Feature text boxes and labels (يسار) txt_MAV = uicontrol('Style','edit','Position',[20 400 80 25],'Enable','inactive'); txt_RMS = uicontrol('Style','edit','Position',[20 360 80 25],'Enable','inactive'); txt_ZC = uicontrol('Style','edit','Position',[20 320 80 25],'Enable','inactive'); uicontrol('Style','text','Position',[20 430 80 20],'String','Features','FontSize',11,'FontWeight','bold'); uicontrol('Style','text','Position',[110 400 50 20],'String','MAV:','FontSize',10); uicontrol('Style','text','Position',[110 360 50 20],'String','RMS:','FontSize',10); uicontrol('Style','text','Position',[110 320 50 20],'String','ZC:','FontSize',10);
% Prediction text box (يسار أعلى الأزرار) txt_pred = uicontrol('Style','text','Position',[20 240 180 30],... 'String','Predicted Gesture: -','FontSize',11,'BackgroundColor','w');
% Motor shaft visualization (يسار تحت) axMotors = axes('Parent',fig,'Position',[0.05 0.05 0.35 0.2]); title(axMotors,'Motor Shafts (CCW Simulation)'); axis(axMotors,[0 6 0 2]); axis(axMotors,'equal'); axis(axMotors,'off'); hold(axMotors,'on');
% Create 5 circular shafts motorCenters = [1 1; 2 1; 3 1; 4 1; 5 1]; shaftRadius = 0.3; motorHandles = gobjects(1,5); for i = 1:5 motorHandles(i) = plot(axMotors, ... motorCenters(i,1)+shaftRadius*[cos(0) cos(pi/2)], ... motorCenters(i,2)+shaftRadius*[sin(0) sin(pi/2)], ... 'k','LineWidth',4); plot(axMotors,motorCenters(i,1),motorCenters(i,2),'ok','MarkerFaceColor','k'); % center end
% Define which motors (fingers) move for each gesture % order = [Thumb Index Middle Ring Pinky] gestureMotors = { [1 2 3 4 5], % Fist → all [1 2 3 4 5], % Open → all [2], % Point → only index [2 3], % Peace → index + middle [1] % ThumbsUp → only thumb };
% Simulated EMG data (2 electrodes) fs = 200; t = 0:1/fs:1; reps = 3; nElectrodes = 2; rawData = zeros(nGestures*reps,length(t),nElectrodes); labels = zeros(nGestures*reps,1); for g=1:nGestures for r=1:reps idx = (g-1)*reps + r; for e=1:nElectrodes amp = 0.2 + 0.15*e + 0.1*g; freq = 5 + g + 0.5*e + 0.2*r; rawData(idx,:,e) = amp*sin(2*pi*freq*t) + 0.05*randn(1,length(t)); end labels(idx) = g; end end
% Train neural network features = []; targets = []; for i=1:size(rawData,1) sig = squeeze(rawData(i,:,:)); sigF = preprocessEMG(sig,fs); featVec = extractFeatures(sigF); features = [features; featVec]; tVec = zeros(1,nGestures); tVec(labels(i)) = 1; targets = [targets; tVec]; end trainedNN = feedforwardnet(20); trainedNN = train(trainedNN,features',targets');
% Create gesture buttons in rows of 3 buttonWidth = 80; buttonHeight = 35; hSpacing = 10; vSpacing = 10; % spacing buttonsPerRow = 3; startX = 20; startY = 200;
for i = 1:nGestures row = floor((i-1)/buttonsPerRow); col = mod((i-1),buttonsPerRow); xPos = startX + col*(buttonWidth+hSpacing); yPos = startY - row*(buttonHeight+vSpacing); uicontrol('Style','pushbutton', ... 'String',gestures{i}, ... 'Position',[xPos yPos buttonWidth buttonHeight], ... 'FontSize',10, ... 'Callback',@(src,evt) executeGesture(i)); end
%% --- Functions --- function executeGesture(idx) % Hand drawing fingers = getFingersFromGesture(idx); plotHand(fingers,axHand);
% EMG signal (2 electrodes) sig = squeeze(rawData(idx,:,:)); cla(axEMG); hold(axEMG,'on'); colors = ['r','b']; labelsElectrodes = {'Electrode 1','Electrode 2'}; for e=1:nElectrodes plot(axEMG,t,sig(:,e),colors(e),'LineWidth',1.2); end legend(axEMG,labelsElectrodes,'Location','northeast'); xlabel(axEMG,'Time (s)'); ylabel(axEMG,'Amplitude'); title(axEMG,'EMG Signal'); grid(axEMG,'on');
% Preprocessing sigF = preprocessEMG(sig,fs);
% Feature extraction featVec = extractFeatures(sigF);
% Display features set(txt_MAV,'String',num2str(featVec(1),'%.3f')); set(txt_RMS,'String',num2str(featVec(2),'%.3f')); set(txt_ZC,'String',num2str(featVec(3),'%.0f'));
% Prediction (currently direct match) set(txt_pred,'String',['Predicted Gesture: ',gestures{idx}]);
% --- Simulated Motor Shafts CCW Rotation --- nSteps = 15; % animation steps thetaStep = -pi/12; % CCW activeMotors = gestureMotors{idx}; % select motors for this gesture
for step = 1:nSteps for m = activeMotors x = motorCenters(m,1) + shaftRadius * [cos(step*thetaStep) cos(step*thetaStep + pi/2)]; y = motorCenters(m,2) + shaftRadius * [sin(step*thetaStep) sin(step*thetaStep + pi/2)]; set(motorHandles(m),'XData',x,'YData',y); end drawnow; pause(0.05); end end
function fingers = getFingersFromGesture(idx) switch idx case 1, fingers = [0 0 0 0 0]; % Fist case 2, fingers = [1 1 1 1 1]; % Open case 3, fingers = [0 1 0 0 0]; % Point case 4, fingers = [0 1 1 0 0]; % Peace case 5, fingers = [1 0 0 0 0]; % ThumbsUp otherwise, fingers = [0 0 0 0 0]; end end
function preprocessed = preprocessEMG(signal,fs) [b,a] = butter(4,[20 90]/(fs/2)); preprocessed = filtfilt(b,a,double(signal)); preprocessed = (preprocessed - mean(preprocessed))/std(preprocessed); end
function feat = extractFeatures(signal) nCh = size(signal,2); feat = []; for ch=1:nCh x = signal(:,ch); MAV = mean(abs(x)); RMSval = rms(x); ZC = sum(diff(x>0)~=0); feat = [feat MAV RMSval ZC]; end end
function plotHand(fingers,ax) cla(ax); hold(ax,'on'); baseX = [2 3 4 5 6]; baseY = 2*ones(1,5); fingerLength = 6; for i = 1:5 angle = fingers(i)*pi/2; x = [baseX(i) baseX(i) - fingerLength*sin(angle)]; y = [baseY(i) baseY(i) + fingerLength*cos(angle)]; plot(ax,x,y,'k','LineWidth',6); end axis(ax,[0 10 0 10]); axis(ax,'equal'); axis(ax,'off'); end
end
0 Kommentare
Siehe auch
Kategorien
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!