How can I assign numeric values to strings in an array?

조회 수: 27 (최근 30일)
Nathanael Zakreski
Nathanael Zakreski 2016년 7월 8일
편집: Stephen23 2016년 7월 8일
I need to assign specific values to some of these chemical species in an array. I need to set RH equal to 100, OH = 100, and NO = 100 and the rest of the species are equal to 0. The array of species looks like this:
SpeciesName =
'RH'
'OH'
'RO2'
'H2O'
'NO'
'NO2'
'RCHO'
'HO2'
'HNO3'
'H2O2'
'O2'
'ROOH'
'hv'
'O3'

채택된 답변

Thorsten
Thorsten 2016년 7월 8일
You can use a structure
S = struct('Name', {{'RH' 'OH' 'RO2' 'H20'}}, 'Value', [100 100 0 0])

추가 답변 (1개)

Stephen23
Stephen23 2016년 7월 8일
편집: Stephen23 2016년 7월 8일
SpeciesName = {
'RH'
'OH'
'RO2'
'H2O'
'NO'
'NO2'
'RCHO'
'HO2'
'HNO3'
'H2O2'
'O2'
'ROOH'
'hv'
'O3'};
C = SpeciesName';
C(2,:) = num2cell(zeros(size(C)));
C(2,1:2) = {100};
S = struct(C{:});
And accessing the data in the structure is trivial:
>> S.O2
ans =
0
>> S.RH
ans =
100
>> S.NO
ans =
0

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by