How can I use a string to load a variable stored in a .mat-file? [duplicate] - matlab

If for instance I have a variable xa=2, and then I construct a string by joining 'x' and 'a', how can I make this new string have the value 2?
xa=2;
var=strcat('x','a');
The result of this is var=xa, but what I want is var=2.
Thank you

Use eval():
var = eval(strcat('x','a'));
It will "evaluate" the string 'xa' and translate it to the value of the variable xa.
Source : MATLAB documentation

Related

How to take integer input from user in dart

i am new in dart and just want to know how to take integer input from user in dart with null safety. i found out a way to take number input from dart which is:
String? chossenNumber = stdin. readLineSync();
if(chossenNumber !=null)
{
int number = int.parse(chossenNumber);
}
but i am unable to use number variable outside of the scope. Please tell me a way to solve this issue.
You can define the variable at the top of the class and initialize it here so you will be able to use it everywhere in the class
The solution of it very simple just take input of number as String i.e
String? chossenNumber = stdin. readLineSync();
and when you want to use this variable parse it to the 'int' i.e
if(int.parse(chossenNumber) <100)
{
print("Your Statement");
}

Dart: How to force string interpolation on a variable

I have a variable that contains a string with interpolated variables. In the code below, that variable is template. When I pass this variable to generateString function, I want to apply string interpolation on it because the values which interpolated variables require are available in generateString function only.
void main() {
String template = '<p>\${name}</p>';
var res = generateString(template);
}
generateString(template) {
var name = 'abc';
print(template);
return template;
}
The problem is when I am printing and returning template inside generateString fn, I am getting <p>${name}</p> instead of <p>abc</p>. Is there a way to explicitly tell the dart to so string interpolation?
I am new to Dart. I don't know if it is even possible to achieve or not. Please suggest how do I do this.
Edit: Based on the inputs from other users, I would like to make a clarification about the scenario presented. The value of template variable is not a string literal. I get that from UI as a user input. I have shown it here as a string literal for code simplicity. Also, please consider that name and template are not in the same scope in my scenario.
The other answers so far are wrong.
String interpolation (looking for $, etc) happens only while compiling from the source code to the value in memory. If that string in turn also has a $, it's no longer special.
It's not possible to trigger interpolation past the original compilation step. You can write a templating system that would look for something like {{name}} in the value, and replace it with the current value of name.
If you have the template and the variable in the same scope, it works as expected.
// evaluate variable inside ${}
var sport = 'basketball';
String template = 'I like <p>${sport}</p>';
print(template);
I didn't fully understand your question maybe this will help
void main() {
print(generateString('abc')); //<p>abc</p>
}
generateString(String template) {
return r"<p>" "$template" r"</p>";
}
Walter White here.
You must define the variable name as global var, so it can "cook" the string for you

Cannot cast from BigDecimal to String in jasper (weird error //$JR_EXPR_ID=18$)

I have a jrxml file which has a field of BigDecimal value and when I try to execute the report. I get an error called " Cannot cast from BigDecimal to String in jasper". Im doing the report using ireport 5.6.0, and I get a weird error like this,
1. Cannot cast from BigDecimal to String
value = (java.lang.String)(((java.math.BigDecimal)variable_variable1.getValue())); //$JR_EXPR_ID=18$
<----------------------------------------------------------------------->
2. Cannot cast from BigDecimal to String
value = (java.lang.String)(((java.math.BigDecimal)variable_variable1.getOldValue())); //$JR_EXPR_ID=18$
<-------------------------------------------------------------------------->
3. Cannot cast from BigDecimal to String
value = (java.lang.String)(((java.math.BigDecimal)variable_variable1.getEstimatedValue())); //$JR_EXPR_ID=18$
<-------------------------------------------------------------------------------->
3 errors
I tried to change the expression class. but nothing seems to work. I need the value to get the sum and also as a field.
You have a field e.g $F{ID} with field class=java.math.BigDecimal.As you want to use this field as a string as well as decimal;
You can create two variables variable1 with variable Class=java.math.BigDecimal with Variable Expression as $F{ID}
and create another variable variable2 with variable Class=java.lang.String with Variable Expression as $F{ID}.toPlainString()
When I removed tag "printWhenExpression" for that text field with error, the error did not show again.

Struct field name by referencing to a value in MATLAB

I am currently working on a struct in MATLAB and have a question regarding this.
Let us say i have declared a struct:
structVariable=struct('abc',[],'cde',[])
i.e.
structVariable =
abc: []
cde: []
Further I have a char variable,
charVariable='abc';
Now, I am trying to use structVariable.abc with something like
structVariable.charVariable =5;
but this does not work. Is it possible to reference to the value of charVariable with something like &charVariable as in c++ ?
This seems to be the easiest way:
structVariable.(charVariable) = 5;
To set the field value:
setfield(structVariable,charVariable,5)
To get the field value:
getfield(structVariable,charVariable)

Dynamic property names in VBA

I have a custom class module in VBA (Access) that is supposed to handle a large amount of external data. Currently I have two functions Read(name) and Write(name, value) that allows to read and set dynamic properties.
Is there a way to define a more syntactic way to read and write those data? I know that some objects in VBA have a special way of accessing data, for example the RecordSet, which allows to read and set data using myRS!property_name. Is there a way to do exactly the same for custom class modules?
The exclamation mark syntax is used to access members of a Scripting.Dictionary instance(you'll need to add a reference to Microsoft Scripting Runtime through Tools > References first). To use this syntaxyou'll need to be storing the information internally in a dictionary.
The quickest way to use it in a class is to give your class an object variable of type Scripting.Dictionary and set it up as follows:
Option Explicit
Dim d As Scripting.Dictionary
Private Sub Class_Initialize()
Set d = New Scripting.Dictionary
End Sub
Private Sub Class_Terminate()
Set d = Nothing
End Sub
Public Property Get IntData() As Scripting.Dictionary
Set IntData = d
End Property
Now you can access properties using myinstance.IntData!MyProperty = 1... but to get to where you want to be you need to use Charlie Pearson's technique for making IntData the default member for your class.
Once that's done, you can use the following syntax:
Dim m As MyClass
Set m = New MyClass
Debug.Print "Age = " & m!Age ' prints: Age =
m!Age = 27
Debug.Print "Age = " & m!Age ' prints: Age = 27
Set m = Nothing
Okay, thanks to Alain and KyleNZ I have now found a working way to do this, without having a collection or enumerable object below.
Basically, thanks to the name of the ! operator, I found out, that access via the bang/pling operator is equivalent to accessing the default member of an object. If the property Value is the default member of my class module, then there are three equivalent statements to access that property:
obj.Value("param")
obj("param")
obj!param
So to make a short syntax working for a custom class module, all one has to do is to define a default member. For example, I now used the following Value property:
Property Get Value(name As String) As String
Value = SomeLookupInMyXMLDocument(name)
End Property
Property Let Value(name As String, val As String) As String
SetSomeNodeValueInMyXMLDocument(name, val)
End Property
Normally, you could now access that like this:
obj.Value("foo") = "New value"
MsgBox obj.Value("foo")
Now to make that property the default member, you have to add a line to the Property definition:
Attribute Value.VB_UserMemId = 0
So, I end up with this:
Property Get Value(name As String) As String
Attribute Value.VB_UserMemId = 0
Value = SomeLookupInMyXMLDocument(name)
End Property
Property Let Value(name As String, val As String) As String
Attribute Value.VB_UserMemId = 0
SetSomeNodeValueInMyXMLDocument(name, val)
End Property
And after that, this works and equivalent to the code shown above:
obj("foo") = "New value"
MsgBox obj("foo")
' As well as
obj!foo = "New value"
MsgBox obj!foo
' Or for more complex `name` entries (i.e. with invalid identifier symbols)
obj![foo] = "New value"
MsgBox obj![foo]
Note that you have to add the Attribute Value.VB_UserMemId = 0 in some other editor than the VBA editor that ships with Microsoft Office, as that one hides Attribute directives for some reason.. You can easily export the module, open it in notepad, add the directives, and import it back in the VBA editor. As long as you don't change too much with the default member, the directive should not be removed (just make sure you check from time to time in an external editor).
See this other question: Bang Notation and Dot Notation in VBA and MS-Access
The bang operator (!) is shorthand for
accessing members of a Collection or
other enumerable object
If you make your class extend the Collection class in VBA then you should be able to take advantage of those operators. In the following question is an example of a user who extended the collection class:
Extend Collections Class VBA