replace a character in a string
LeChat
If I have a string like
my_string_VALUE
and I want to make a loop on the value of
VALUE
from 0.0 to 0.99 while replacing the dot by the letter p (as I guess nobody wants a wave name with a dot), how can I proceed?The result I need should be strings of form:
my_string_0p00 my_string_0p01 my_string_0p02... my_string_0p99
Thank you so much,
ReplaceString(replaceThisStr, inStr, withThisStr [, caseSense [, maxReplace ] ])
It should do what you want.
HJ
Edit: You don't even need a loop:
Generate a dummy wave
Edit/K=1 'tst'
change the periods
and have a look at the changes in the table
February 22, 2017 at 07:07 am - Permalink
though I think there is no need of [p] in the command you indicate:
tst=replacestring(".",tst,"p")
seems to also work :)
Thank you for your quick and accurate help!
February 22, 2017 at 07:30 am - Permalink
This doesn't quite work because it give 0.0, 0.1, 0.2 ... 0.10 etc
Make /T tst="asdf_" + num2str(p/100)
Is closer to what you want, although the first point is still not right.
You can also do this in a loop and pad the variable in the string so that 0.01 to 0.09 come first.
February 22, 2017 at 07:30 am - Permalink
HJ
February 22, 2017 at 07:45 am - Permalink
replacestring
if you have already written the string variable/ wave.If you insist on creating the string within a loop, I think the best will be to just concatenate strings, i.e.:
string s_prefix = "my_string_0"
variable v_value, v_max = 100
for (v_value=0; v_value<v_max; v_value+=1)
s_value = s_prefix + replacestring(".", num2str(v_value/100), "p")
endfor
But the above code will search _every time_ for the "." position. Chances are it is never going to be different. Not ideal.
So this can be optimized. But this will depend on the value.
For example, if the value is in the interval
[0-100)
(as you showed), I would use:s_prefix = "my_string_0p"
for ..
s_value = s_prefix + num2str(v_value)
endfor ..
You can take a look at
sprintf
for formatted printing to a string.As to "doing it without a loop", using
make
: I think make uses a loop itself. It populates (hopefully efficiently) the waves with some sort of a loop using the dimension provided (128 if not provided for rows; ignored for other dimensions), i.e.:where
p
andq
are the loop variables. And there are different loops for different loop variables. I might as well be wrong here though.best,
_sk
February 23, 2017 at 12:53 am - Permalink
Are you doing a string list? Then, you can use ReplaceString without a loop. With VALUE as 0.0 to 0.99, just to replace the dot, use "." to "p" as ...
Example
mypday;todaypday;thep0pday;
I might suggest to replace the dot by "_" (underscore) or "-" (dash) rather than p when these are filenames.
--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAH
February 22, 2017 at 10:45 am - Permalink
This list will still need to be unpacked to individual wave names with, for example,
stringfromlist(i, "mypday;todaypday;thep0pday", ";")
, wherei
is the loop variable. So a loop is again needed.Loops are not bad ;-).
best,
_sk
February 23, 2017 at 12:52 am - Permalink
Actually this is what I finally did :)
tst=replacestring(".",tst,"p")
Thanks for your help!
February 23, 2017 at 03:14 am - Permalink
Try this:
You get:
a0.001s
a0.011s
a0.021s
a0.031s
a0.041s
a0.051s
a0.061s
a0.071s
.....
Then
You get:
a0p01s
a0p11s
a0p21s
a0p31s
a0p41s
a0p51s
a0p61s
a0p71s
...
Then
You get:
a0p00
a0p01
a0p02
a0p03
a0p04
a0p05
a0p06
a0p07
...
August 30, 2018 at 10:55 am - Permalink