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

7 次查看(过去 30 天)
Hello,
is there a way to know through Matlab if the laptop is currently plugged in AC power or running on battery?
Thanks,
Mark

采纳的回答

Les Beckham
Les Beckham 2019-12-15
编辑:Les Beckham 2019-12-15
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 个评论
Les Beckham
Les Beckham 2019-12-29
Yes. Quite unfortunate, as I found this question quite interesting to investigate and experiment with. Happy holidays to you and yours, Walter.

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2019-12-15
编辑:Walter Roberson 2019-12-15
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 个评论
Mark Golberg
Mark Golberg 2019-12-15
I receive the follwoing :
msg =
''pmset' is not recognized as an internal or external command,
operable program or batch file.
'
Am I missing something?
Walter Roberson
Walter Roberson 2019-12-15
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by