MATLAB Answers

How can I know if my laptop is plugged into AC power via Matlab?

17 views (last 30 days)
Mark Golberg
Mark Golberg on 15 Dec 2019
Commented: Mark Golberg on 29 Dec 2019
Hello,
is there a way to know through Matlab if the laptop is currently plugged in AC power or running on battery?
Thanks,
Mark

Accepted Answer

Les Beckham
Les Beckham on 15 Dec 2019
Edited: Les Beckham on 15 Dec 2019
That error message (''pmset' is not recognized as an internal or external command, operable program or batch file) is a Windows error message.
Based on this blog post you can determine this in Windows using an undocumented/unsupported class in Powershell:
Put this Powershell script wherever you wish (like in your "Documents" folder, for instance):
Function Test-IsOnBattery
{
Param(
[string]$computer
)
if ([BOOL](Get-WmiObject -Class BatteryStatus -Namespace root\wmi -ComputerName $computer).PowerOnLine)
{Write-Output "On AC Power"}
else
{Write-Output "On Battery"}
} #end function test-IsOnBattery
Test-IsOnBattery -computer <YourComputerName>
Name the file Test-IsOnBattery.ps1.
Then call it from Matlab like this:
[status, output] = system("powershell C:/Users/<YourUserName>/Documents/Test-IsOnBattery");
Then you can test the result like this:
if strfind(output, "AC")
disp('AC')
else
disp('Battery')
end
I hope this helps.
  7 Comments
Mark Golberg
Mark Golberg on 29 Dec 2019
OS - windows 10.
Les Beckham solution worked as a charm.
THANK YOU!

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 15 Dec 2019
Edited: Walter Roberson on 15 Dec 2019
You can adapt that in MATLAB by using system() of the pmset piped to head, and receive the result as the second output of system, and do the string testing in MATLAB.
  4 Comments
Walter Roberson
Walter Roberson on 15 Dec 2019
That is very odd. The only way I can think of that that could happen is is if you installed something like Parallels in compatibility mode and then accidentally linked /bin/bash to /Applications/Parallels13.02.app/contents/bin/command.exe -- but if you had done that I would tend to doubt that you would have been able to boot all the way.
Hmmm... No, even if you were using R2012a with OS 10.15 that error message does not make any sense, too many other things would have to have gone wrong.
Is your laptop "Late 2012" or would it happen to be "Mid 2012" or earlier? MATLAB does happen to be compatible with selected Mid 2012 models but not Early 2012 models (not if you are using R2019b and OS 10.15); I ask because we just might need to guide you through using the Recovery Partition to fix /bin/bash but the mid 2012 models did not yet have Recovery Partition and have to be recovered from DVD.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!