MATLAB Answers

Using cellfun in app designer

1 view (last 30 days)
Jes Dreier
Jes Dreier on 8 Jan 2020
Commented: Jes Dreier on 31 Jan 2020
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);
if iscell(cell2)
cell2 = cell2mat(cell2);
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'))];
The call I am making is:
[app.file,app.path,idx] = uigetfile('*.csv','Select all the applicable csv files','MultiSelect','on');
if idx == 0
%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));
startend = [14,9];
testfile = app.file;
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?


Sign in to comment.

Answers (1)

Sahithi Kanumarlapudi
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

  1 Comment

Jes Dreier
Jes Dreier on 31 Jan 2020
Hi Sahithi
Thank you very much for taking the time to answer my question. What you write if of course correct, but not relevant for my issue. The cellfun is inside and if statement that check to see whether the output is a cell(array), becausethe output is char array if only one file is selected. So that is not what is causing the issue. As I also mentioned I have no problem getting it to work out side of the app designer enviorment.
However doing a little more digging I found that it complains when typing it into the appdesigner:
To call a superclass method, the method name 'cellfun' must match the name of the subclass method 'LoadcsvfilesButtonPushed'.
I tried my best to google this and read the help function about super classes, but it still didn't help me.
I would still very much appriciate any help you can give, in terms of solving this issue.

Sign in to comment.