Rewrite the format of a string

6 views (last 30 days)
Tatjana Mü
Tatjana Mü on 12 Jul 2022
Commented: Karim on 12 Jul 2022
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 Comment
Stephen23
Stephen23 on 12 Jul 2022
A = ["A","B","B","C";"E","F","F","G"]
A = 2×4 string array
"A" "B" "B" "C" "E" "F" "F" "G"
B = cellfun(@(s)join(unique(s),','),num2cell(A,2))
B = 2×1 string array
"A,B,C" "E,F,G"

Sign in to comment.

Answers (1)

Karim
Karim on 12 Jul 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{:})
iwant = 28104×1 string array
"Ag,As,Cd,Co,Cr,Cs,Cu,Dy,Er,Fe,Ga,Gd,Ge,Hf,Hg,I,K,Kr,Mg,Mn,Mo,Na,Ni,Os,P,Pb,Pt,Rb,Ru,Sc,Se,Si,Sn,Sr,Te,Tl,U,V,W,Xe,Yb,Zn,Zr" "Cr,Cs,Fe,Ga,Ge,K,Kr,Mg,Mn,Na,Ni,P,Rb,Sc,Si,Sr,V,Zn" "Al,Ba,Ce,Cl,Ga,Ge,K,La,Na,P,Rb,Sc,Si,Xe" "Al,Ba,Cd,Ce,Fe,Ga,Ge,K,La,Na,Nd,Ni,Os,P,Pd,Pt,Rb,Sc,Se,Si,Sn,Sr,Ta,W,Xe" "Al,Ca,Kr,Lu,Na,Rb,Sc,Si,Sr" "Al,Ba,Ca,Ce,Dy,Ga,Kr,La,Na,Rb,Sc,Si,Sr,Xe" "Al,Ca,Na,Sc,Si,Sr" "Al,Ba,Cd,Ce,Cl,Cr,Cu,Dy,Er,Fe,Ga,Gd,Ge,Hf,Hg,Ho,Ir,K,La,Lu,Mo,Na,Nd,Ni,Pb,Pd,Pr,Rb,S,Se,Ta,Th,V,W,Xe,Yb,Zr" "Al,Ba,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