This question already has answers here:
Create variables with names from strings
(6 answers)
Closed 7 years ago.
Assume the variable FileName contains a string such as Name1. How do I make a variable with the name Name1?
The example 4 at this page seems to be similar, but I cant get it to work. Is it the right way to do it?
http://se.mathworks.com/help/matlab/ref/genvarname.html
What you see in "Example 4" is accused as bad programming style. The documentation also contains a section why to avoid eval.
I would recommend a struct with dynamic field names to achieve similar.
filename='name1';
mydata=struct();
mydata.(genvarname(filename))=load(filename);
Besides better performance, you also get additional functionality when handling multiple files. For example structfun to apply a function to all your data or fieldnames to get all filenames.
For what you want to do, the eval function is there for you:
FileName = 'Name1';
eval([FileName ' = 18;']); % Executes "Name1 = 18;"
and now the variable Name1 is created and has a value of 18.
The function genvarname has a different purpose, which is to generate acceptable and non-conflicting variable names, and not the variables themselves.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
What does this line do in perl?
my %input=CTRL()->{cgi}->Vars;
I don't really understand whats going on here. My best assumption is that Vars is the key and we are inserting those key-value pairs into the input hash.
It's called CTRL. This function returns a reference to a hash. It access the element with keys cgi of this hash. It appears to be an instance of CGI. It calls the Vars method of that object.
Many people want to fetch the entire parameter list as a hash in which the keys are the names of the CGI parameters, and the values are the parameters' values. The Vars() method does this. Called in a scalar context, it returns the parameter list as a tied hash reference. Changing a key changes the value of the parameter in the underlying CGI parameter list. Called in a list context, it returns the parameter list as an ordinary hash. This allows you to read the contents of the parameter list, but not to change it.
When using this, the thing you must watch out for are multivalued CGI parameters. Because a hash cannot distinguish between scalar and list context, multivalued parameters will be returned as a packed string, separated by the "\0" (null) character. You must split this packed string in order to get at the individual values. This is the convention introduced long ago by Steve Brenner in his cgi-lib.pl module for perl version 4, and may be replaced in future versions with array references.
This question already has answers here:
Swift variable name with ` (backtick)
(3 answers)
Closed 2 years ago.
What does the `` around default mean in the following code below?
In what cases would you want to use it?
static let `default` = User(id: UUID(), name: "Anonymous")
Thank you in advanced
The backticks allow you to use restricted keywords in places where they otherwise couldn't be used, such as variable names.
default is a restricted keyword in Swift, but by putting backticks around it, it becomes a valid variable name.
Without the backticks, your code would result in the error
Keyword 'default' cannot be used as an identifier here
I am working with Stata.
I have a variable called graduate_secondary.
I generate a global variable called outcome, because eventually I will use another outcome.
Now I want to replace the variable graduate if a condition relative to global is met, but I get an error:
My code is:
global outcome "graduate_secondary"
gen graduate=.
replace graduate=1 if graduate_primary==1 & `outcome'==1
But i receive the symbol ==1 invalid name.
Does anyone know why?
Something along those lines might work (using a reproducible example):
sysuse auto, clear
global outcome "rep78"
gen graduate=.
replace graduate=1 if mpg==22 & $outcome==3
(2 real changes made)
In your example, just use
replace graduate=1 if graduate_primary==1 & $outcome==1
would work.
Another solution is to replace global outcome "graduate_secondary" with local outcome "graduate_secondary".
Stata has two types of macros: global, which are accessed with a $, and local, which are accessed with single quotes `' around the name -- as you did in your original code.
You get an error message because a local by the name of outcome has no value assigned to it in your workspace. By design, this will not itself produce an error but instead will the reference to the macro will evaluate as a blank value. You can see the result of evaluating macro references when you type them by using display as follows. You can also see all of the macros in your workspace with macro dir (the locals start with an underscore):
display `outcome'
display $outcome
Here is a blog post about using macros in Stata. In general, I only use global macros when I have to pass something between multiple routines, but this seems like a good use case for locals.
This question already has answers here:
Is it possible to define more than one function per file in MATLAB, and access them from outside that file?
(9 answers)
Closed 7 years ago.
How can i write code after defining a function in matlab?
I would have separated them but I have to submit my homework as whole, one .m file.
It gives the following error "This statement is not inside any function.". I tried the answers in the web but it wont work.
My .m file starts with defining the function and after the end statement, after the definition I start writing my code. And I use my function defined above in the code
Thanks very much.
You should do it the other way round.
Just wrap your 'normal' script in a function. Then, the other functions you can declare at the end of the file. E.g., if your file is called myHomework.m
function myHomework() % Should match the filename!
n=6;
if n>5
x = someFunction(n);
disp(x);
end
end % Although this 'end' can usually be omitted, not now!
function out=someFunction(in)
...
end
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Suppressing a function’s command window output
Suppress Output
Is there a way to "silence" the output of a Matlab function? In other words, if a function generates some displayed text in the command window, is there a way to run it in a quiet mode, where the output is suppressed?
In my case, I am using a third-party function iteratively that displays a lot of text, and I want to find a way to suppress that text without modifying the function itself. I'm thinking there must be some kind of wrapper function like quiet(thirdpartyFunction) that gives this kind of behavior. Or is this wishful thinking?
You can probably use evalc and discard the return value.