How can I pass variables to eval without error suppression ?

조회 수: 2 (최근 30일)
Patrick Bernier
Patrick Bernier 2019년 10월 17일
댓글: Patrick Bernier 2019년 10월 18일
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
Patrick Bernier
Patrick Bernier 2019년 10월 18일
@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 2019년 10월 18일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2019년 10월 18일
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)

추가 답변 (1개)

Walter Roberson
Walter Roberson 2019년 10월 17일
sprintf('%g', a, b);
This will not work for non-numeric variables.
  댓글 수: 4
Patrick Bernier
Patrick Bernier 2019년 10월 18일
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 2019년 10월 18일
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')

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by