loop for species names

조회 수: 2 (최근 30일)
Michal Sleszynski
Michal Sleszynski 2021년 11월 21일
댓글: Star Strider 2021년 11월 21일
So i need to use a for loop to generate the name of the species.
i would need to use CnH2n+2 for n:1 to 5 and the output should be in form below as i dont need variable names here i need a string
'CH4' , 'C2H6' and so on until C5H12'
it would be preferable that it is an array so that i can access those names by indexing

채택된 답변

Star Strider
Star Strider 2021년 11월 21일
Try this —
Species = @(n) strrep(compose('C%dH%d', [n(:) 2*n(:)+2]), '1H','H');
Hydrocarbon = Species(1:5)
Hydrocarbon = 5×1 cell array
{'CH4' } {'C2H6' } {'C3H8' } {'C4H10'} {'C5H12'}
Back to my undergraduate organic chemistry days ...
.
  댓글 수: 2
Michal Sleszynski
Michal Sleszynski 2021년 11월 21일
nice, thank you it works perfectly
Star Strider
Star Strider 2021년 11월 21일
As always, my pleasure!
.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by