cellfun in new version

조회 수: 2 (최근 30일)
ori yungrise
ori yungrise 2018년 11월 26일
댓글: ori yungrise 2018년 11월 27일
I am trying to update an old code.
The code has many lines with "cellfun" function and i use anonymous functions.
It worked well at R2012b version but it dowsnt work at R2018a version.
examples:
  • cellfun(@(x) logical(exist(x,'var')), handles.Names); % . The names in it are variables that exist in the workspace.
  • cellfun(@(x) eval(['handles.' x ' =cell(size(Size_Parameter));']), handles.Names);
  • cellfun(@(x) save([FullFileName],'-struct','handles',x,'-append'),handles.Names);
handles.Names is cell array in my GUI.
All these work well in R2012b but not in R2018a.
I will be glad to get some help. Do i have to replace all these command lines into loops?
  댓글 수: 6
Walter Roberson
Walter Roberson 2018년 11월 27일
for ii=1:numel(handles.Names)
handles.(handles.Names{ii}) = cell(size(Size_Parameter));
end
In the case where the handles struct starts out empty, then there is another way to do it in vectorized form involving cell2struct(), or a different way involving struct() and structfun(). There are probably other ways as well for that situation. They do not, however, apply for the case where handles already has content.
ori yungrise
ori yungrise 2018년 11월 27일
Thank you, this is great!

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

채택된 답변

Walter Roberson
Walter Roberson 2018년 11월 26일
cellfun(@(x) logical(exist(x,'var')), handles.Names)
The exist() is going to be executed in the context of a function. It will test the existence of the given name as a variable in the workspace of the function, then as a parameter to the function (second), then as a shared variable in a nested function, then as a global variable that has been declared as global in the function. But the function context that the exist() is being executed in is the anonymous function, which has no saved variables and has one parameter named 'x' and is not a nested function and has no global declared in it. The only variable name that exists will therefore be 'x'. If there happened to be a 'x' in handles.Names then that one will succeed and all others will fail.
In order for this anonymous function to do anything more useful it would need to use evalin('caller')
If you do not need to know about parameters or shared variables, then you could instead
ismember(handles.Names, who())
  댓글 수: 1
ori yungrise
ori yungrise 2018년 11월 27일
편집: ori yungrise 2018년 11월 27일
Hi Walter,
I wil use: "ismember(handles.Names, who())" and then "logical".
Any ideas about the other two comman lines?
Thank you!

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by