I am integrating code from someone else that is written as
MACRO(addr) = c
Where this did something like *addr = c
Due to a change in the underlying processor I need to call a __builtin function to handle the data differently such as
NEW_MACRO(addr,c) is there a way to write:
#define MACRO(a)=c NEW_MACRO(a,c) ?
Thanks,
Martin
Why don't you do the opposite?
#define NEW_MACRO(a,c) (MACRO(a)=c)
Related
I have a function with the form:
function t = fun(str1, str2, str3)
I'm trying to figure out a way to have the arguments passed to it as one cell containing the 3 arguments. Is there a way to do this? i.e.:
args = {str1, str2, str3};
x = fun(args);
I'm trying to find something that sets up this type of functionality. I know I could theoretically do fun(args{1}, args{2}, args{3}) but this isn't quite what I had in mind. I know varargs would work, but only if I were to change the function inputs, which I hope not to have to do. Anyway, thanks for the help.
How about
args = {str1, str2, str3};
x = fun(args{:});
I have a situation in MATLAB where I want to try to assign a struct field into a new variable, like this:
swimming = fish.carp;
BUT the field carp may or may not be defined. Is there a way to specify a default value in case carp is not a valid field? For example, in Perl I would write
my $swimming = $fish{carp} or my $swimming = 0;
where 0 is the default value and or specifies the action to be performed if the assignment fails. Seems like something similar should exist in MATLAB, but I can't seem to find any documentation of it. For the sake of code readability I'd rather not use an if statement or a try/catch block, if I can help it.
You can make your own function to handle this and keep the code rather clear. Something like:
swimming = get_struct(fish, 'carp', 0);
with
function v = get_struct(s, f, d)
if isfield(s, f)
v = s.(f); % Struct value
else
v = d; % Default value
end
Best,
From what I know, you can't do it in one line in MATLAB. MATLAB logical constructs require explicit if/else statements and can't do it in one line... like in Perl or Python.
What you can do is check to see if the fish structure contains the carp field. If it isn't, then you can set the default value to be 0.
Use isfield to help you do that. Therefore:
if isfield(fish, 'carp')
swimming = fish.carp;
else
swimming = 0;
end
Also, as what Ratbert said, you can put it into one line with commas... but again, you still need that if/else construct:
if isfield(fish,'carp'), swimming = fish.carp; else, swimming = 0;
Another possible workaround is to declare a custom function yourself that takes in a structure and a field, and allow it to return the value at the field, or 0.
function [out] = get_field(S, field)
if isfield(S, field)
out = S.(field);
else
out = 0;
end
Then, you can do this:
swimming = get_field(fish, 'carp');
swimming will either by 0, or fish.carp. This way, it doesn't sacrifice code readability, but you'll need to create a custom function to do what you want.
If you don't like to define a custom function in a separate function file - which is certainly a good option - you can define two anonymous functions at the beginning of your script instead.
helper = {#(s,f) 0, #(s,f) s.(f)}
getfieldOrDefault = #(s,f) helper{ isfield(s,f) + 1 }(s,f)
With the definition
fish.carp = 42
and the function calls
a = getfieldOrDefault(fish,'carp')
b = getfieldOrDefault(fish,'codfish')
you get for the first one
a = 42
and the previous defined default value for the second case
b = 0
Is there a way here to write more efficient, more generic code?
As the value return doesn't change, do I need a switch here?
function result = whichValue(value)
switch value
case 'green_ok'
result = 'green&ok';
case 'green_ko'
result = 'green&ko';
case 'green_check'
result = 'green&check';
end
end
This seems like a poor way of doing things, but seems to work for your examples:
result=value
value(value=='_')='&'
How about using strsplit:
value_split = strsplit(value, '_');
result = [value_split{1}, '&', value_split{2}]
I need to declare two different constants in my app
one is a simple string, the other needs to be a uint32.
I know of two different ways to declare constants as follows
#define VERSION 1; //I am not sure how this works in regards to uint32.. but thats what I need it to be.
and
NSString * const SIGNATURE = #"helloworld";
is there a way to do the version which should be a uint32 like the nsstring decliration below?
for instance something like
UInt32 * const VERSION 1;
if so how? if not, how do i make sure the #define version is of type uint32?
any help would be appreciated
You're very close. The correct syntax is:
const UInt32 VERSION = 1;
You can also use UInt32 const rather than const UInt32. They're identical for scalars. For pointers such as SIGNATURE, however, the order matters, and your order is correct.
You're confused by macro definitions & constants:
#define VERSION (1)
or
#define SOME_STRING #"Hello there"
The above are macro definitions. This means during compilation VERSION & SOME_STRING will be replaced with the defined values all over the code. This is a quicker solution, but is more difficult to debug.
Examples of constant declarations are:
const NSUInteger VERSION = 1;
NSString * const RKLICURegexException = #"Some string";
Look at the constants like simple variables that are immutable and can't change their values.
Also, be careful with defining pointers to constants & constant values.
i want to define something similar to a computation method:
NSString *format = #"%d + 1";
In my code i want to do something like:
int computedNumber = sum(format,5) => the result should be 6.
could you give some suggestions?
thank you
EDIT:
or something similar to:
NSNumber *no = [NSNumber numberWithFormat:format,5];
It is not normally possible, however there have been written parsers for this specific tasks, to mention DDMathParser by SO user Dave DeLong.
But for what task do you really need this? You have the + operator, and then you perform the sum function? Couldn't you simply parse the number at the end of the format, then perform the operation you'd like?
Using Macro can be an alternative solution to your problem. You can define a macro like the following,
#define INC(a) (a + 1)
Here, INC and a are user-defined. You can give them any name you want. Compiler will substitute (a + 1) in your code, where ever you call INC(a). For example, consider the following,
int computedNumber = INC(5);
After the compilation the code will be,
int computedNumber = (5 + 1); // the result during the execution is 6.