how to convert a variable to string in matlab? [duplicate] - string

This question already has answers here:
print variable-name in Matlab
(4 answers)
Closed 9 years ago.
I have structure array
some_struct_var=struct( 'filed1', filed1, 'filed2', filed2 ,...)
I want to create a string
str=['The struct variable name is :' , some_struct_var]
with the name of the structure variable in it. The some_struct_var may vary and is not fixed.

Create a function that takes any variable as an input and returns the string equivalent of that variable's name as an ouput like so:
varToStr = #(x) inputname(1);
structVarString = varToStr(some_struct_var)
str = ['The struct variable name is :', structVarString]

Related

Why did "None" came as the second output for my one line code in python? [duplicate]

This question already has answers here:
Why is this printing 'None' in the output? [duplicate]
(2 answers)
Why does the print function return None?
(1 answer)
Closed last year.
print(print("Hello World!"))
and the output is :
Hello World!
None
The print function has no return value, i.e. it returns None.
(What did you expect to get with the second print?)

How to treat given integer as binary values? [duplicate]

This question already has answers here:
Convert base-2 binary number string to int
(10 answers)
Closed 2 years ago.
I have a integer value i.e. 1010110. How to treat this value as binary? So that i can find the integer value of that binary value.
You can pass a base parameter (2 in this case) to the int function:
s = "1010110"
i = int(s, 2)
# 86

Apply f-string interpolation to a string [duplicate]

This question already has answers here:
How to postpone/defer the evaluation of f-strings?
(14 answers)
Closed 3 years ago.
Is there a way to achieve applying f-strings interpolation dynamically on a string?
For example given the string a = '{len([1])}'
a.interpolate() to give '1'
Try this:
a = f'{len([1])}'
print(a)
#1

How to store numbers of a string in a list in python [duplicate]

This question already has answers here:
Convert string (without any separator) to list
(9 answers)
Closed 7 years ago.
I have a string s="12345678"
I need to store these numbers in a list like below format.
s=['1','2','3','4','5','6','7','8']
So can anybody help me how to do this.
Thanks,
Try this:
s = "12345678"
a = [c for c in s]

Convert string to table in lua [duplicate]

This question already has answers here:
String to Table in Lua
(2 answers)
Closed 9 years ago.
I am newbie in lua. I need to convert following string to lua table. How could I do this?
str = "{a=1, b=2, c={d=3,e=4} }"
I want to convert this string to lua table, so that I can access it like this:
print(str['a']) -- Output : 1
print(str['c']['d']) -- Output : 3
You could simply add a str = to the beginning of the string and let the interpreter load that string as a chunk for you. Note that loadstring doesn't run the chunk but returns a function. So you add () to call that function right away and actually execute the code:
loadstring("str = "..str)()
This would do the same thing:
str = loadstring("return "..str)()
If you don't generate the string yourself, that can be dangerous though (because any code would be executed). In that case, you might want to parse the string manually, to make sure that it's actually a table and contains no bad function calls.

Resources