Using cellfun in app designer
1 次查看(过去 30 天)
显示 更早的评论
Hi
I am wokring in the app designer and is trying to run a function on cell array using cellfun function. I am running it on a cell array of file names I got from the uiinput function.
The function is defined in the metode section and is (I have tried with both with and without the first input ( ~/app):
function startend = commonnames(~,cell1,cell2)
if iscell(cell1)
cell1 = cell2mat(cell1);
end
if iscell(cell2)
cell2 = cell2mat(cell2);
end
commonLen = min(length(cell1),length(cell2));
lodicalarrayfwd = ~(cell1(1:commonLen) == cell2(1:commonLen));
lodicalarraybck = ~(cell1(end-(commonLen-1):end) == cell2(end-(commonLen-1):end));
startend = [find(lodicalarrayfwd,1)-1,((commonLen-1)-find(lodicalarraybck,1,'last'))];
end
The call I am making is:
[app.file,app.path,idx] = uigetfile('*.csv','Select all the applicable csv files','MultiSelect','on');
if idx == 0
return
end
%generate a start guess for the image file name
if iscell(app.file)
startend = cell2mat(cellfun(@commonnames, app.file, app.file(end:-1:1),'UniformOutput',false));
startend = min(startend)
testfile = cell2mat(app.file(1));
else
startend = [14,9];
testfile = app.file;
end
The error I am getting is "Error using cellfun : Undefined function 'commonnames' for input arguments of type 'char'."
I am guessing the issue is that a function defined in the methode section need app as first input (at least else it shows up wiht an error), but you can't pass the app using the cellfun function.
I have tried deleting the ~ input and just run as function startend = commonnames(cell1,cell2) but that doesn't work either and I get the same error.
I have run the function in a regular script and there it runs without issue, so I am guessing it is connected to the app designer.
is that app designer simple not compatiable with cellfun unless you use a anonymous function? or is there something I am missing here?
0 个评论
回答(3 个)
Sahithi Kanumarlapudi
2020-1-22
‘uigetfile' returns a character vector if a single file is selected. It returns a cell array of character vectors only when 'MultiSelect' is set to 'on' and a user selects multiple files. This error might be resulted if you have selected only one file. You could refer the following link for more information
大輝 渡辺
2021-6-8
Hi Jes,
just now, I got the completely same problem with you.
The 1st argument (app) mgiht affect the execution for "cellfun".
Here, i have two arguments, these are 'app' and 'cell type'.
This 'app' argument must be inserted for the 1st argument...
yep....I have no answer, very sorry..
0 个评论
Mohsin Zubair
2022-11-10
Although this is 2 year old question but as I see it hasn't been resolved and I recently encountered same problem but with varfun in app designer, I couldn't get it to work with (app) input as I couldn't fully understand underlying coding or didn't even go too deep into it, but I did resolve the general issue another way, gonna answer it here in case anyone else need it:
What you must do is, instead of defining your user-defined function in methods of app designer just define your function inside the original call back where you are using the cellfun/varfun etc. then it works perfectly !
In context to this original question, the code would be like following:
[app.file,app.path,idx] = uigetfile('*.csv','Select all the applicable csv files','MultiSelect','on');
if idx == 0
return
end
%generate a start guess for the image file name
if iscell(app.file)
startend = cell2mat(cellfun(@commonnames, app.file, ...
app.file(end:-1:1),'UniformOutput',false));
startend = min(startend)
testfile = cell2mat(app.file(1));
else
startend = [14,9];
testfile = app.file;
end
%% now here at the end of this callback write
function startend = commonnames(cell1,cell2)
if iscell(cell1)
cell1 = cell2mat(cell1);
end
if iscell(cell2)
cell2 = cell2mat(cell2);
end
commonLen = min(length(cell1),length(cell2));
lodicalarrayfwd = ~(cell1(1:commonLen) == cell2(1:commonLen));
lodicalarraybck = ~(cell1(end-(commonLen-1):end) == cell2(end-(commonLen-1):end));
startend = [find(lodicalarrayfwd,1)-1,((commonLen-1)-find(lodicalarraybck,1,'last'))];
end
In case if you need to use app inside your user defined function then don't worry app works globally inside the call back so you don't need to make any changes and use app inside function easily.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Develop Apps Using App Designer 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!