设为首页收藏本站

最大的系统仿真与系统优化公益交流社区

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 2907|回复: 0

[交流] 如何在方程中处理字符串变量

[复制链接]
发表于 2009-4-10 13:43:02 | 显示全部楼层 |阅读模式
转自 ExtendSim 英文论坛
: Z, G5 E# n+ w; f4 F# U1 \) w& _; l. P& s1 c
Question:
$ P$ m; Y/ ]: d. x$ @4 u
/ H0 _% j  a/ M& c) a, s2 nI know that you can use the underlying integer for a string attribute within an equation block.  However, I would like to be able to use the string value.6 U- Q( ?9 E: C7 V/ d
For example if I have a string attribute called "State" with possible values "PA" and "NC" I know the following equation would work properly:$ {) Q2 }- g  r( t# V- T
if (State == 1) then% u% e& ]' \4 D+ U' r: J
   outCon0 = 1;
& b+ T% T$ {4 k- e; s# T" relseif (State == 2) then
: L) q2 @$ @# U9 Z# T   outCon0 = 2;6 A$ h- `; |" q3 q( n
else3 [) h" M5 ^- S! s* B
   outCon0 = 0;7 b1 y" l$ e  m
However, I want the following to work but don't know how in Extend
1 _" j  s( h3 {# M, q4 Dif (State == "PA") then
0 k1 ]' q0 d$ `  y9 V7 ]3 o   outCon0 = 1;
, W. a; m8 ^7 ?4 |. p$ Eelseif (State == "NC") then/ c  X* h; S- M9 |$ B5 V  O) \& r
   outCon0 = 2;
8 q, V% w$ _: D  Y& q! ielse
, T. ~: T( S8 C- ?7 {, R   outCon0 = 0;
* x/ {2 e7 h) z; c
' b7 t* w( g1 Z, q
- T7 W9 f- K" D. l9 ^" l+ B; kAnswer: 6 E$ s3 ]+ y0 x7 O( J4 G+ G

! p! F! \% g$ g7 {' \' LThat's a good question. In ExtendSim string attributes are stored as integers which are used as indexes into arrays of strings. This is efficient for both performance and memory. The following equation will get the string from the attribute:/ u, n% w1 W8 q; `
6 r! f$ z: K7 X, B
if(SLStringGet("State", State) == "NC") Path = 0;( T( T% I0 ?: a" X* W! e
else if(SLStringGet("State", State) == "PA") Path = 1;$ ]! D" A  b0 W  }: Q2 i. w& J
else abort;
. X2 v+ f8 |2 o0 d2 x1 u$ f6 x( }9 g% v
I put the abort in at the end so if there was a string that was not in the equation, it would be caught by the model.

评分

参与人数 1仿真币 +20 +2 收起 理由
focuscon + 20 + 2

查看全部评分

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|手机版|SimulWay 道于仿真   

GMT+8, 2024-11-22 21:45 , Processed in 0.015894 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表