Using cellfun in app designer

3 vues (au cours des 30 derniers jours)
Jes Dreier
Jes Dreier le 8 Jan 2020
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?

Réponses (3)

Sahithi Kanumarlapudi
Sahithi Kanumarlapudi le 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 commentaire
Jes Dreier
Jes Dreier le 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.

Connectez-vous pour commenter.


大輝 渡辺
大輝 渡辺 le 8 Juin 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..

Mohsin Zubair
Mohsin Zubair le 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.

Catégories

En savoir plus sur Software Development Tools dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by