Matlab实现采集电脑的CPU等硬件信息

matlab实现采集电脑的cpu等硬件信息

Matlab实现采集电脑的CPU等硬件信息

不多废话,直接展示代码

代码语言:javascript代码运行次数:0运行复制```javascript function info = cpuinfo() % CPU数据采集 % 信息 = CPUINFO()返回一个包含不同字段的结构 % 中央处理器和操作系统信息由/proc/cpuinfo(Unix)、 % sysctl(Mac)或WMIC(Windows)提供。 % CPU名称 % CPU的时钟频率 % CPU缓存大小(L2) % 物理CPU核心数 % * 操作系统名称和版本 % % 参见: computer, isunix, ismac

if isunix % 判断是否为UNIX系统 if ismac % 判断是否为Mac系统 info = cpuInfoMac(); else info = cpuInfoUnix(); end else info = cpuInfoWindows(); end

%------------------------------------------------------------------------- function info = cpuInfoWindows() sysInfo = callWMIC('cpu'); osInfo = callWMIC('os'); info = struct( ... 'Name', sysInfo.Name, ... 'Clock', [sysInfo.MaxClockSpeed, ' MHz'], ... 'Cache', [sysInfo.L2CacheSize, ' KB'], ... 'NumProcessors', str2double(sysInfo.NumberOfCores), ... 'OSType', 'Windows', ... 'OSVersion', osInfo.Caption); % 输出结构

%------------------------------------------------------------------------- function info = callWMIC(alias) % 调用Windows管理命令行WMIC olddir = pwd(); % 保存当前目录 cd(tempdir); % 切换到临时目录 sysinfo = evalc(sprintf('!wmic %s get /value', alias)); % 执行WMIC命令 cd(olddir); % 切换回原目录 fields = textscan(sysinfo, '%s', 'Delimiter', '\n'); fields = fields{1}; % 处理数据 fields(cellfun('isempty', fields)) = []; % 去除空行 values = cell(size(fields)); for ff = 1:numel(fields) idx = find(fields{ff} == '=', 1, 'first'); % 查找等号 if ~isempty(idx) && idx > 1 values{ff} = strtrim(fields{ff}(idx+1:end)); % 提取值 fields{ff} = strtrim(fields{ff}(1:idx-1)); % 提取字段名 end end % 删除任何重复项(仅适用于双插槽电脑,我们假设所有插槽有相同的处理器)。 numResults = sum(strcmpi(fields, fields{1})); if numResults > 1 % 计算核心数 numCoresEntries = find(strcmpi(fields, 'NumberOfCores')); if ~isempty(numCoresEntries) cores = cellfun(@str2double, values(numCoresEntries)); values(numCoresEntries) = {num2str(sum(cores))}; end % 去除重复结果 [fields, idx] = unique(fields, 'first'); values = values(idx); end % 数据转换 info = cell2struct(values, fields);

%------------------------------------------------------------------------- function info = cpuInfoMac() % Mac系统与Windows类似 machdep = callSysCtl('machdep.cpu'); hw = callSysCtl('hw'); info = struct( ... 'Name', machdep.brand_string, ... 'Clock', [num2str(str2double(hw.cpufrequency_max)/1e6), ' MHz'], ... 'Cache', [machdep.cache.size, ' KB'], ... 'NumProcessors', str2double(machdep.core_count), ... 'OSType', 'Mac OS/X', ... 'OSVersion', getOSXVersion());

%------------------------------------------------------------------------- function info = callSysCtl(namespace) infostr = evalc(sprintf('!sysctl -a %s', namespace)); % 移除前缀 infostr = strrep(infostr, [namespace, '.'], ''); % 现在转换为结构 infostr = textscan(infostr, '%s', 'delimiter', '\n'); infostr = infostr{1}; info = struct(); for ii = 1:numel(infostr) colonIdx = find(infostr{ii} == ':', 1, 'first'); if isempty(colonIdx) || colonIdx == 1 || colonIdx == length(infostr{ii}) continue end prefix = infostr{ii}(1:colonIdx-1); value = strtrim(infostr{ii}(colonIdx+1:end)); while ismember('.', prefix) dotIndex = find(prefix == '.', 1, 'last'); suffix = prefix(dotIndex+1:end); prefix = prefix(1:dotIndex-1); value = struct(suffix, value); end info.(prefix) = value; end

%------------------------------------------------------------------------- function vernum = getOSXVersion() % 提取操作系统版本号 ver = evalc('system(''sw_vers'')'); vernum = regexp(ver, 'ProductVersion:\s([1234567890.]*)', 'tokens', 'once'); vernum = strtrim(vernum{1});

%------------------------------------------------------------------------- function info = cpuInfoUnix() txt = readCPUInfo(); cpuinfo = parseCPUInfoText(txt); txt = readOSInfo(); osinfo = parseOSInfoText(txt); % 合并结构 info = cell2struct([struct2cell(cpuinfo); struct2cell(osinfo)], ... [fieldnames(cpuinfo); fieldnames(osinfo)]);

%------------------------------------------------------------------------- function info = parseCPUInfoText(txt) % 解析字段 lookup = { ... 'model name', 'Name' ... 'cpu Mhz', 'Clock' ... 'cpu cores', 'NumProcessors' ... 'cache size', 'Cache' ... }; info = struct( ... 'Name', {''}, ... 'Clock', {''}, ... 'Cache', {''} ); for ii = 1:numel(txt) if isempty(txt{ii}) continue; end % 查找分隔属性名和值的冒号 colon = find(txt{ii} == ':', 1, 'first'); if isempty(colon) || colon == 1 || colon == length(txt{ii}) continue; end fieldName = strtrim(txt{ii}(1:colon-1)); fieldValue = strtrim(txt{ii}(colon+1:end)); if isempty(fieldName) || isempty(fieldValue) continue; end % 是否为我们感兴趣的字段之一? idx = find(strcmpi(lookup(:,1), fieldName)); if ~isempty(idx) newName = lookup{idx,2}; info.(newName) = fieldValue; end end % 转换时钟速度 info.Clock = [info.Clock, ' MHz']; % 转换核心数 info.NumProcessors = str2double(info.NumProcessors);

%------------------------------------------------------------------------- function info = parseOSInfoText(txt) info = struct( ... 'OSType', 'Linux', ... 'OSVersion', ''); % 查找字符串"linux version"然后查找括号内的内容 [~, b] = regexp(txt, '[^(](([^)])).*', 'match', 'tokens', 'once'); info.OSVersion = b{1}{1};

%------------------------------------------------------------------------- function txt = readCPUInfo() fid = fopen('/proc/cpuinfo', 'rt'); if fid

输出结果如下

代码语言:javascript代码运行次数:0运行复制```javascript >> info = cpuinfo() info = 包含以下字段的 struct: Name: '12th Gen Intel(R) Core(TM) i5-12500H' Clock: '2500 MHz' Cache: '9216 KB' NumProcessors: 12 OSType: 'Windows' OSVersion: 'Microsoft Windows 11 家庭中文版'

网友留言(0 条)

发表评论