matlab定义变量var,设置变量数据类型 - MATLAB setvartype - MathWorks 中国
使用 detectImportOptions 创建导入选项,设置多个变量的数据类型,然后使用 readtable 读取数据。
创建一个选项对象。
opts = detectImportOptions('patients.xls');
检查变量的当前(检测到的)数据类型。
disp([opts.VariableNames' opts.VariableTypes'])
{'LastName' } {'char' }
{'Gender' } {'char' }
{'Age' } {'double' }
{'Location' } {'char' }
{'Height' } {'double' }
{'Weight' } {'double' }
{'Smoker' } {'logical'}
{'Systolic' } {'double' }
{'Diastolic' } {'double' }
{'SelfAssessedHealthStatus'} {'char' }
根据您的导入需要更改多个变量的数据类型。
opts = setvartype(opts,{'LastName','Gender','Location',...
'Smoker','SelfAssessedHealthStatus'},'string');
opts = setvartype(opts,{'Age','Height','Weight',...
'Systolic','Diastolic'},'single');
检查更新后的变量数据类型。
disp([opts.VariableNames' opts.VariableTypes'])
{'LastName' } {'string'}
{'Gender' } {'string'}
{'Age' } {'single'}
{'Location' } {'string'}
{'Height' } {'single'}
{'Weight' } {'single'}
{'Smoker' } {'string'}
{'Systolic' } {'single'}
{'Diastolic' } {'single'}
{'SelfAssessedHealthStatus'} {'string'}
使用 readtable 导入更新类型之后的变量。
T = readtable('patients.xls',opts);
更多推荐


所有评论(0)