How can I pass variables to eval without error suppression ?
Ältere Kommentare anzeigen
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 Kommentare
Guillaume
am 17 Okt. 2019
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.
Patrick Bernier
am 18 Okt. 2019
Patrick Bernier
am 18 Okt. 2019
Akzeptierte Antwort
Weitere Antworten (1)
Walter Roberson
am 17 Okt. 2019
sprintf('%g', a, b);
This will not work for non-numeric variables.
4 Kommentare
Patrick Bernier
am 17 Okt. 2019
Walter Roberson
am 18 Okt. 2019
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.
Patrick Bernier
am 18 Okt. 2019
Walter Roberson
am 18 Okt. 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')
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!