File Exchange

image thumbnail

CPU Info

version 1.3 (4.04 KB) by Ben Tordoff
Read various bits of information about the CPU and operating system.


Updated 22 Apr 2021

From GitHub

View Version History

View license on GitHub

Editor's Note: This file was selected as MATLAB Central Pick of the Week

This function reads various bits of information about the CPU and operating
system, including:

* CPU name
* CPU clock speed
* CPU Cache size (L2)
* Total memory size (bytes)
* Number of physical CPU cores
* Operating system name & version

These are provided by /proc/cpu (Unix), sysctl (Mac) or WMIC (Windows).

Requires MATLAB R2016b or above.

Cite As

Ben Tordoff (2021). CPU Info (, GitHub. Retrieved .

Comments and Ratings (9)

Ben Tordoff

Thanks for the suggestions. I can deifnitely add total memory and apply the fix for multi-socket systems. I'm not sure about unique identifiers - serial numbers only existed for a short time on old Intel CPUs. Hostname is possible - I'll take a look.

Daniel Dolan

This remains a great function for determining what MATLAB is running on. Two requests:
-It would be nice to return the total system memory. For Macs, this information is available from hw = callSysCtl( 'hw' ) in the "memsize" field. I am not sure about Linux and Windows.
-Sometimes one might want to know if a program is running on a specific machine. Maybe CPUs no longer have serial numbers, but some kind of device ID must be available. Perhaps the host ID used for MATLAB activation?

Max Chandler

Great function! I've modified it for the machines I use which have multiple CPUs, where this only detects the first CPU.

Multi Socket motherboards in Linux can be added in the following way :

function info = parseCPUInfoText( txt )
% Now parse the fields
lookup = {
'model name', 'Name'
'cpu Mhz', 'Clock'
'cpu cores', 'NumCores'
'cache size', 'Cache'
'physical id', 'NumCPUs'};
info = struct( ...
'Name', {''}, ...
'Clock', {''}, ...
'Cache', {''} );
for ii=1:numel( txt )
if isempty( txt{ii} )
% Look for the colon that separates the property name from the value
colon = find( txt{ii}==':', 1, 'first' );
if isempty( colon ) || colon==1 || colon==length( txt{ii} )
fieldName = strtrim( txt{ii}(1:colon-1) );
fieldValue = strtrim( txt{ii}(colon+1:end) );
if isempty( fieldName ) || isempty( fieldValue )

% Is it one of the fields we're interested in?
idx = find( strcmpi( lookup(:,1), fieldName ) );
if ~isempty( idx )
newName = lookup{idx,2};
info.(newName) = fieldValue;

% Convert clock speed
info.Clock = [info.Clock, ' MHz'];

% Convert num cores
info.NumCores = str2double( info.NumCores );

% convert num cpus and figure out total core count
info.NumCPUs = str2num(info.NumCPUs) + 1;
info.TotalCores = info.NumCPUs * info.NumCores;


Matthew Kelly

Nice Job!
This did exactly what I was looking for.


Ben Tordoff

Hi Daniel, I don't know much about CPU serial numbers, but from what I've read they don't exist in modern CPUs. See e.g.:

When I ask for the serial on my CPUs (on linux) I just get "none specified".

It might be possible to get the motherboard serial number, although I haven't managed to get it on Linux without sudo'ing (not a problem on Windows as far as I can tell).

What is it you want to achieve?

Cheers. Ben

Daniel Dolan

This function does exactly what it says it should. Is there a way to pull a serial number from the CPU?


MATLAB Release Compatibility
Created with R2021a
Compatible with R2016b and later releases
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!