Error using ismember function: Input A of class cell and input B of class cell must be cell arrays of character vectors, unless one is a character vector.

9 vues (au cours des 30 derniers jours)
I have a cell array of names with some duplicates. For example, the 1st, 18th and 32nd row of the array contain the same value. I am looking to use each name in the array to create and name a csv file.
Since there are duplicates, I am not able to save a file for each row of the array because each duplicate overwrites the previous file with that name.
My solution was to append an '_i' to the name every time I came accross a duplicate. So if there was a second duplicate, it would be renamed with a '_i_i' at the end of it.
My issue is that the array I am creating, "duplicates", is mixing doubles with cell array values. For example: { 1x1 double 1x1 double {'cell'} }
This is why I added the section of code that says "duplicates(e) = {'none'}". However, it is not filling in those spots with {'none'}. Why isn't this functioning correctly? My apologies in advance for the long tedious explanation.
build8dn = build8dn';
[build8dataname,ia] = unique(build8dn);
for e = 1:length(input_files)
test = input_filenames{e};
testnum = test(1,5:6);
duplicates = {};
if ismember(e,ia)
duplicates(e) = {'none'};
build8data_names{1,1}{str2num(testnum)} = strcat(build8data_names{1,1}{str2num(testnum)},'_i');
duplicates(e) = build8data_names{1,1}{str2num(testnum)};
if ismember(duplicates(e),duplicates)
dup = strcat(duplicates(e),'_i');
build8data_names{1,1}{str2num(testnum)} = dup;
filename = strcat(build8data_names{1,1}{str2num(testnum)},'_',bcstr,'.csv');
input_files_new = input_files(e);
input_files_new = cell2mat(input_files_new);
filename = cell2mat(filename);
  1 commentaire
G A le 1 Juil 2021
May be instead of duplicates = {}; before if-statement use duplicates = {''};?

Connectez-vous pour commenter.

Réponse acceptée

Mohammad Sami
Mohammad Sami le 2 Juil 2021
I suggest you keep your logic simple. just append the loop number in your filename, thereby avoiding the complicated logic altogether.

Plus de réponses (0)


En savoir plus sur Matrices and Arrays dans Help Center et File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by