Using cellfun in app designer
3 views (last 30 days)
Show older comments
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 Comments
Answers (3)
Sahithi Kanumarlapudi
on 22 Jan 2020
‘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
大輝 渡辺
on 8 Jun 2021
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 Comments
Mohsin Zubair
on 10 Nov 2022
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 Comments
See Also
Categories
Find more on Software Development Tools in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!