How can I pass variables to eval without error suppression ?

2 views (last 30 days)
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
Patrick Bernier
Patrick Bernier on 18 Oct 2019
@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 ?
Patrick Bernier
Patrick Bernier on 18 Oct 2019
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

Guillaume
Guillaume on 18 Oct 2019
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)

Walter Roberson
Walter Roberson on 17 Oct 2019
sprintf('%g', a, b);
This will not work for non-numeric variables.
  4 Comments
Patrick Bernier
Patrick Bernier on 18 Oct 2019
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)'
Walter Roberson
Walter Roberson on 18 Oct 2019
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!