How can I pass variables to eval without error suppression ?

I'm trying to not use the error suppression on a line and to eliminate the console output of a function using evalc.
a = 1; % Matlab tells me this value might be unused.
b = [1 1]; %ok<NASGU> <- I'm also trying to not use those where possible.
evalc('fun(length(b),b,a)');
Is there a way to acheive both of my goals ? I feel like I'm either stuck with the console output or the error suppression message.
Thanks for your help.

3 Comments

The obvious question is why are you using evalc (or eval) which we keep on saying is rarely a good idea.
The 2nd question is why do you care so much about whether or not an mlint warning is suppressed or not. It has not influence on what is displayed when the code is run.
In any case, why not use feval:
feval('fun', numel(b), b, a)
and avoid all the problems.
@Guillaume
Thanks for the answer. I'm using evalc to remove the console output. We are calling compiled code that gives a long console output and I'm trying to avoid it. evalc seems to be the only option in matlab.
It's not that I care so much about the mlint warning. I'm curious as to know if it is possible. It helps debugs and makes for clearer, cleaner code imo.
Finally, feval doesn't work as it doesn't suppress the console output.
@ Stephen
Thanks for the editorial, but it leads me nowhere to tell me to write better code.
Can you elaborate how str2func solves my problem ?
I understand your points, and believe me that I am trying my best to solve the tons of issues that came before me at work...
You have to understand, and that was the main point of my reply, that your initial answer to write better code is such a blanket comment that it leaves me still completely clueless. Your initial comment is still perfectly valid without this out-of-context editorial on a code that you have no clue if it is "good" or "bad".
I asked a question on a forum that provides answers and you didn't. I simply pointed it out.

Sign in to comment.

 Accepted Answer

f = @() func(numel(b), a, b); %prepare function for call
evalc('f()'); %call function. () optional but make it clear we're calling a function
limits the content of eval to the struct minimum and avoids the mlint warning about unused variables (except maybe f)

More Answers (1)

sprintf('%g', a, b);
This will not work for non-numeric variables.

4 Comments

This doesn't work as it does not keep my variable b as an array, it splits it.
>> sprintf('%g',a,b)
ans =
'111'
Do you have any advice to pass an array and keep its integrity with sprintf ?
It doesn't matter if it splits b. Notice that I put semicolon at the end of the sprintf. The command executes and the result is thrown away, but the analyzer is happy because it sees that the variables are used.
Walter,
Thanks for your support.
I'm not sure I get how this applies to my situation. I'm trying to pass arrays to evalc via sprinft. Using your method, sprintf doesn't seem to be able to do so.
% What I want to do
>> sum([1 1 1])
ans =
3
% Following your answer (to the point since I couldn't extrapolate easily)
>> sprintf('sum(%g)',[1 1 1])
ans =
'sum(1)sum(1)sum(1)'
This is a new additional call whose output is intended to be thrown away. The only reason to add it is to silence the analyzer warning.
a=whatever
b=whatever
sprintf('%g', a, b); %use a and b to silence analysis
evalc('whatever')

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!