Way for matching letters and numbers in two files?

Hello,
I have a question about one code. I have two files. file1, has one column with numbers only. the second file (file2) has two column. in which the first column has numbers and the second columnn has letters/characters. I would like to add a new column in file1 which would have letters/characters, based on the file2 structure.
I send you some pics in order to see what I want to do.
file1
file2
final file
I attach the two files (final1.xlsx & file2.xlsx) and the final file (final_file.xlsx) I would like to create
could you help me please?

 Réponse acceptée

Image Analyst
Image Analyst le 22 Déc 2020

0 votes

Not sure how file1 is involved. If you want to sort the cell array file2 based on the contents of column 1, use sortrows(ca, 1). If you need anymore help, attach the two files.

3 commentaires

Ivan Mich
Ivan Mich le 22 Déc 2020
I am attached them. Basically I would like my code:
1) Read file1 and file2
2)based on file 2 matches (eg number 1 corresponts to "a", number 3 to "e" etc) to create a new column in file1.
3)Mainly , depending on the number of 1st column of file1 to write the number which responts to (based on file 2 matces) in an another column
final_file is the file I would like to create via code
Ivan, I'm sure you've figured it out by now, but a simple for loop will do the trick. Here is what I have:
m1 = readmatrix('file1.xlsx')
ca2 = readcell('file2.xlsx')
col2a = cell2mat(ca2(:, 1))
% Make output cell array
ca3 = cell(size(m1, 1), 2);
for row = 1 : size(m1, 1) % For every row in file 1
rowIn2 = find(col2a == m1(row), 1, 'first'); % Find the first matching row.
ca3{row, 1} = m1(row); % Assign the number
ca3{row, 2} = ca2{rowIn2, 2}; % Assign the letter.
end
ca3 % Show in command window
outputFileName = fullfile(pwd, 'file3.xlsx');
writecell(ca3, outputFileName);
If it works, can you please "Accept this answer"?
Ivan Mich
Ivan Mich le 23 Déc 2020
Thank you very much!! It works!!!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by