Rewrite the format of a string
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi,
as usual i have an error in my code. I import several files, from which I get my first string (SP_RL). Just as an example it looks like this:
SP_RL=["A","B","B","C";"E","F","F","G"]
My script should now delete the double values in every row and should rewrite it into another format like this:
iwant=["a,b,c";"e,f,g"]
So I add two example files and my code. Because I dont find the error, which overwrites the "iwant" string after every file.
directory_name=uigetdir('','Ordner mit Messungen auswählen');
[nur_file_name,pfad]=uigetfile({'*.xlsx','xlsx (*.xslx)';'*.*','all Files'},...
'Die xslx-Files oeffnen (probe_001.xlsx=',[directory_name '/'], 'Multiselect', 'on');
nur_file_name=cellstr(nur_file_name);
nur_file_name=sort(nur_file_name);
filename=strcat(pfad,nur_file_name);
anzahl_files=size(filename,2);
for xy=1:anzahl_files
fid_in=fopen(char(filename(xy)),'r');
tmpImport = importdata(filename{xy},',');
SP_RL = tmpImport.Tabelle1;
SP_RL = regexprep(SP_RL,'\''','');
SP_RL = regexprep(SP_RL,'\+','');
SP_RL= regexprep(SP_RL, '\d+(?:_(?=\d))?', '');
[anzahl_zeile,anzahl_elemente]=size(SP_RL);
SP_RL=string(SP_RL);
%---------------------In the following is the error:
for x=1:anzahl_zeile
iwant(x,:) = join(unique(split(string(SP_RL(x,:)))),',');
end
iwant=strip(iwant,'left',",");
iwant=strip(iwant,'right',",");
end
I appreciate any help. Thank you.
1 comentario
Stephen23
el 12 de Jul. de 2022
A = ["A","B","B","C";"E","F","F","G"]
B = cellfun(@(s)join(unique(s),','),num2cell(A,2))
Respuestas (1)
Karim
el 12 de Jul. de 2022
In the for loop, 'x' starts from 1, hence you will overwrite the data in iwant for each new file.
One method to resolve this is to store the data (i.e. "iwant') temporary in a cell array and append al the data at the end of the routine. See below for this method.
MyFiles = ["Smp_1_1_4_2021_11_05_11h41m37s_Zusammensetzung.xlsx"
"Smp_1_1_8_2021_11_05_11h54m49s_Zusammensetzung.xlsx"];
% create a temporary storage for the data from each file, append to one big
% string array in the end
tmpCell = cell(length(MyFiles),1);
for i = 1:length(MyFiles)
tmpImport = importdata(MyFiles(i),',');
SP_RL = tmpImport.Tabelle1;
SP_RL = regexprep(SP_RL,'\''','');
SP_RL = regexprep(SP_RL,'\+','');
SP_RL = regexprep(SP_RL, '\d+(?:_(?=\d))?', '');
[anzahl_zeile,anzahl_elemente] = size(SP_RL);
SP_RL = string(SP_RL);
for x = 1:anzahl_zeile
iwant(x,:) = join(unique(split(string(SP_RL(x,:)))),',');
end
iwant = strip(iwant,'left',",");
iwant = strip(iwant,'right',",");
tmpCell{i} = iwant;
end
iwant = vertcat(tmpCell{:})
4 comentarios
Karim
el 12 de Jul. de 2022
That's because filename in your case is a cell array, try indexing it with curly brackets like this:
tmpImport = importdata(filename{i},',');
Ver también
Categorías
Más información sobre Random Number Generation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!