Rewrite the format of a string
이전 댓글 표시
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
A = ["A","B","B","C";"E","F","F","G"]
B = cellfun(@(s)join(unique(s),','),num2cell(A,2))
답변 (1개)
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
Tatjana Mü
2022년 7월 12일
Karim
2022년 7월 12일
Just like you showed in your original post (see below). The only reason, i changed these lines of code was to let the code run on the forum.
length(MyFiles) is the same as anzahl_files in your code.
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
Tatjana Mü
2022년 7월 12일
Karim
2022년 7월 12일
That's because filename in your case is a cell array, try indexing it with curly brackets like this:
tmpImport = importdata(filename{i},',');
카테고리
도움말 센터 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!