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

조회 수: 8 (최근 30일)
Mark Golberg
Mark Golberg 2019년 12월 15일
댓글: Mark Golberg 2019년 12월 29일
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.
Mark Golberg
Mark Golberg 2019년 12월 29일
OS - windows 10.
Les Beckham solution worked as a charm.
THANK YOU!

댓글을 달려면 로그인하십시오.

추가 답변 (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