Compare two string arrays with a if loop
Mostra commenti meno recenti
Hi,
now it gets complicated. And I reach my limits with Matlab. I have a xlsx file with compositions of particles (SMP_3_16_3.xslx) and a databank of minerals (Datenbank_Minerale.xlsx). These are just test files. Later this files will have more than 30.000 lines.
So the idea is to import both files (which is working fine). I change some formations. Then I want to compare the composition of the particle (column 1 of string array: iwant), with the databank compositions (column 1 of Datenbank_Minerale). If they are equal, the mineral names (saved in column 2-x) should be written next to the composition in the sample file (iwant). Until now, we are not sure how many columns the databank will have, means we dont know how many minerals fit a composition (it will be different from line to line). To my code:
I import the data:
%--------------------Import of sample files--------------------------------
directory_name=uigetdir('','Ordner mit Messungen auswählen');
[nur_file_name,pfad]=uigetfile({'*.xlsx','xlsx (*.xslx)';'*.*','all Files'},...
'Die xslx-Files der Proben 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');
%------------------------Get the data from the files----------------------
tmpImport = importdata(filename{xy},',');
SP_RL = tmpImport.Tabelle1;
SP_RL = regexprep(SP_RL,'\''','');
SP_RL = regexprep(SP_RL,'\+','');
SP_RL = string(SP_RL);
[anzahl_zeile,anzahl_elemente]=size(SP_RL);
%----------------Delete numbers and duplicate of elements-----------------
SP_RL= regexprep(SP_RL, '\d+(?:_(?=\d))?', '');
for x=1:anzahl_zeile
iwant(x,:) = join(unique(split(string(SP_RL(x,:)))),',');
end
iwant=eraseBetween(iwant,1,1);
[anzahl_Reihen,anzahl_Spalten]=size(iwant);
%-----------------------Import databank---------------------------------
directory_name=uigetdir('','Ordner mit Messungen auswählen');
[nur_file_name,pfad]=uigetfile({'*.xlsx','xlsx (*.xlsx)';'*.*','all Files'},...
'Das xlxs-File der Proben oeffnen (probe_001.xlsx=',[directory_name '/']);
RLImport = importdata('Datenbank_Minerale.xlsx');
Datenbank=string(RLImport);
[anzahl_lines,anzahl_columns]=size(Datenbank);
This part is working fine. Now I want to compare it and save the mineral names in the sample file but I reach my limit how to tell matlab that I want to have the mineral names in the sample file.
for i=1:anzahl_Reihen
if strcmp(Datenbank,iwant(i,:));
iwant(i,2:anzahl_columns)= ???
end
end
I hope somebody can help me, because this is so complicated.
Thank you really much
1 Commento
@Tatjana Mü: Simplify your view on the problem:
"So the idea is to import both files (which is working fine)" - than you can omit this part in the description, which mentions the Excel files. Start with some already imported data: "I have a cell array/table containing..."
"Later this files will have more than 30.000 lines." - then they are still small.
This is fragile:
filename=strcat(pfad,nur_file_name);
Prefer fullfile instead of strcat.
Instead of casting a scalar cell string to a CHAR, simply access the contents of the cell string:
% fid_in=fopen(char(filename(xy)),'r');
fid_in=fopen(filename{xy}, 'r');
You do this some linaes later, but fid_in is neither used nor closed. Simply omit the fopen line.
The text of the question until "This part is working fine" is not useful to solve your problem, but typing this has wasted your time only - and the time of the readers. In opposite to this, the explanation of the actual problem is really lean: "compare it and save the mineral names in the sample file".
Your "Datenbank" and "iwant" are string matrices or vectors? Then start with some Matlab code, which creates a tiny but relevant example and post a manually construted output.
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Structures in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!