Effacer les filtres
Effacer les filtres

The code which disconnect safely an USB drive works randomly

16 vues (au cours des 30 derniers jours)
Pierre
Pierre le 11 Juin 2024
Réponse apportée : Pierre le 26 Juin 2024 à 13:30
Dear all,
I work on MATLAB R2023a and Windows 11 23H2.
Thanks to this post I coud do the following code which disconnect safely E:, a USB drive.
Unfortunately, sometimes it does not work, for example, when I change the plug from USB port, it needs several execution to disconnect safely the drive.
I also noticed that in the app (see below), missing run are more frequent. The code is just executed under the button Run (first callback).
The command window shows the message I expect when it works but in fact the USB drive is still visible on "My Computer". If the disconnection works, I see an error message in the command window.
Do you have an idea what can I do to solve this issue or find it origin?
Best regards,
Pierre
The code:
function EjectUSB
% Create the PowerShell command to eject the drive
powershellCommand = '(New-Object -comObject Shell.Application).Namespace(17).ParseName(''E:'').InvokeVerb(''Eject'')';
% Create the command to run PowerShell from MATLAB
command = sprintf('powershell -Command "%s"', powershellCommand);
% Execute the PowerShell command
[status, cmdout] = system(command);
% Display the results
if status == 0
disp(['Lecteur USB E éjecté avec succès.']);
else
disp('Échec de l''éjection du lecteur USB.');
disp(cmdout);
end
end
%Add a diskpart list to check if E: has been removed
The app:
classdef PotVibrantUI < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
AmplitudeSlider matlab.ui.control.Slider
FrequencySlider matlab.ui.control.Slider
Amplitude0to1EditField matlab.ui.control.NumericEditField
Amplitude0to1EditFieldLabel matlab.ui.control.Label
FrequencyHzEditField matlab.ui.control.NumericEditField
FrequencyHzEditFieldLabel matlab.ui.control.Label
RunButton matlab.ui.control.Button
end
properties (Access = private)
frequency = 20; % Default frequency value
amplitude = 0.5; % Default amplitude value
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: RunButton
function RunButtonPushed(app, event)
SetupFileCreation(app.frequency)
CustomFileCreation(app.amplitude)
pause(3)
EjectUSB
pause(3)
EjectUSB
end
% Value changing function: FrequencySlider
function FrequencySliderValueChanging(app, event)
changingValue = event.Value;
app.FrequencyHzEditField.Value = changingValue;
% Update the app.frequency property
app.frequency = changingValue;
% guidata() to share variables
end
% Value changed function: FrequencyHzEditField
function FrequencyHzEditFieldValueChanged(app, event)
value = app.FrequencyHzEditField.Value;
app.FrequencySlider.Value = value;
% Update the app.frequency property
app.frequency = value;
end
% Value changing function: AmplitudeSlider
function AmplitudeSliderValueChanging(app, event)
changingValue = event.Value;
app.Amplitude0to1EditField.Value = changingValue;
% Update the app.frequency property
app.amplitude = changingValue;
% guidata() to share variables
end
% Value changed function: Amplitude0to1EditField
function Amplitude0to1EditFieldValueChanged(app, event)
value = app.Amplitude0to1EditField.Value;
app.AmplitudeSlider.Value = value;
app.amplitude = value;
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'MATLAB App';
% Create RunButton
app.RunButton = uibutton(app.UIFigure, 'push');
app.RunButton.ButtonPushedFcn = createCallbackFcn(app, @RunButtonPushed, true);
app.RunButton.FontSize = 24;
app.RunButton.Position = [26 41 568 80];
app.RunButton.Text = 'Run';
% Create FrequencyHzEditFieldLabel
app.FrequencyHzEditFieldLabel = uilabel(app.UIFigure);
app.FrequencyHzEditFieldLabel.HorizontalAlignment = 'right';
app.FrequencyHzEditFieldLabel.FontSize = 24;
app.FrequencyHzEditFieldLabel.Position = [49 435 170 31];
app.FrequencyHzEditFieldLabel.Text = 'Frequency (Hz)';
% Create FrequencyHzEditField
app.FrequencyHzEditField = uieditfield(app.UIFigure, 'numeric');
app.FrequencyHzEditField.ValueChangedFcn = createCallbackFcn(app, @FrequencyHzEditFieldValueChanged, true);
app.FrequencyHzEditField.FontSize = 24;
app.FrequencyHzEditField.Position = [234 434 100 32];
app.FrequencyHzEditField.Value = 20;
% Create Amplitude0to1EditFieldLabel
app.Amplitude0to1EditFieldLabel = uilabel(app.UIFigure);
app.Amplitude0to1EditFieldLabel.HorizontalAlignment = 'right';
app.Amplitude0to1EditFieldLabel.FontSize = 24;
app.Amplitude0to1EditFieldLabel.Position = [38 252 195 31];
app.Amplitude0to1EditFieldLabel.Text = 'Amplitude (0 to 1)';
% Create Amplitude0to1EditField
app.Amplitude0to1EditField = uieditfield(app.UIFigure, 'numeric');
app.Amplitude0to1EditField.ValueChangedFcn = createCallbackFcn(app, @Amplitude0to1EditFieldValueChanged, true);
app.Amplitude0to1EditField.FontSize = 24;
app.Amplitude0to1EditField.Position = [248 251 100 32];
app.Amplitude0to1EditField.Value = 0.5;
% Create FrequencySlider
app.FrequencySlider = uislider(app.UIFigure);
app.FrequencySlider.Limits = [10 40];
app.FrequencySlider.ValueChangingFcn = createCallbackFcn(app, @FrequencySliderValueChanging, true);
app.FrequencySlider.FontSize = 24;
app.FrequencySlider.Position = [26 418 473 3];
app.FrequencySlider.Value = 20;
% Create AmplitudeSlider
app.AmplitudeSlider = uislider(app.UIFigure);
app.AmplitudeSlider.Limits = [0 1];
app.AmplitudeSlider.MajorTicks = [0 0.125 0.25 0.375 0.5 0.625 0.75 0.875 1];
app.AmplitudeSlider.ValueChangingFcn = createCallbackFcn(app, @AmplitudeSliderValueChanging, true);
app.AmplitudeSlider.FontSize = 18;
app.AmplitudeSlider.Position = [20 207 479 3];
app.AmplitudeSlider.Value = 0.5;
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = PotVibrantUI
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end

Réponses (2)

Divyanshu
Divyanshu le 26 Juin 2024 à 4:07
Hi Pierre,
A possible reason for the above code working randomly to eject USB, can be because of the function 'EjectUSB'.
Although the callback attached to 'Run' button might be triggered every time you click the button in the App but because of the improper functioning of 'EjectUSB' function issue occurs.
You can modify the logic of 'EjectUSB' function and can use 'diskpart' utility as specified in the edited part of the original solution:
Hope it helps!

Pierre
Pierre le 26 Juin 2024 à 13:30
Hi Divyanshu,
Thanks a lot for your suggestion. I found out that "remove all dismount" is a little bit too strong, it actually delete the letter of the drive so it is not detected anymore in the explorer.
See what happen below:
So, when I plug the USB again, Diskpart detect it as Volume 5 but not the explorer. I have to assign a letter again:
Best regards,
Pierre

Catégories

En savoir plus sur Develop uifigure-Based Apps dans Help Center et File Exchange

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by