adding different number of columns to specific row
3 views (last 30 days)
Show older comments
Hi,
I have 2 differnt csv files, i want to append the columns from book8 to book 7 based on the name column that appears in the 2 files, the name in 7 are unique but in 8 there are duplicates, so the appending columns may differ from row to another ...
is there a way to add the corresponding columns to a specific row?
i tried to append the columns in differnt ways with no success...
[M_obs,text_obs,obs]=xlsread('book7.csv');
[M_pri,text_pri,pri]=xlsread('book8.csv');
[r,c]=size(obs);
[new_matrix]=deal({});
new_matrix=obs;
for row=(2:r) %
z=find(strcmp(pri(row,1),obs(:,1)))
new_matrix(row,:)=pri(z,3:end);
end
0 Comments
Answers (1)
Ridwan Alam
on 30 Jan 2020
Edited: Ridwan Alam
on 30 Jan 2020
Try using the table join() method:
Example:
M_obs = readtable('book7.csv');
M_pri = readtable('book8.csv');
[~,M_pri_idx] = unique(M_pri(:,1));
M_pri_new = M_pri(M_pri_idx,:);
new_table = join(M_obs,M_pri_new,'Keys',[1,1]);
Hope this helps.
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!