Effacer les filtres
Effacer les filtres

How can I merge a row from one table to a second table with more data based on name?

2 vues (au cours des 30 derniers jours)
I have multiple csv files containing data. One of these files contains a column of image names and then a corresponding x and y coordinate. The rest of the files are named the image name but containing multiple x and y coordinates.
Is there a way that I can take a title from the first file, find the file named that and make a new table containing the first xy coordinates and then the rest from the second file? Can I get it to work through all of my files?
EDIT: this is what I have at the moment:
files = dir('*.txt') ; % get all text files of the folder
N = length(files) ; % Toatl number of files
for i = 1:N % loop for each file
thisfile = files(i).name ; % present file
%%do what you want
D= load(i);
%%Find matching row in T
name = string(thisfile);
j = (Results.Label == thisfile);
%%New table with T
% Add columns from .txt
T=table(D.XM(i), D.YM(i))
end
Some example files attached!
  2 commentaires
Guillaume
Guillaume le 27 Sep 2019
An example of the text file containing the names of the other files, and an example of said other file would help.
The code you wrote doesn't make much sense, in particular the load(integervalue), and doesn't appear to try to do what you're asking.
Asvin Kumar
Asvin Kumar le 30 Sep 2019
Can you please clarify on how you want to create the new table?

Connectez-vous pour commenter.

Réponses (1)

Guillaume
Guillaume le 30 Sep 2019
it's not clear what you want the new table to be, nor what you want to do afterward with it (save it?). Here's a start. Adapt as needed:
resultfile = 'C:\somewfolder\Results.xlsx'; %location of result file
filefolder = 'C:\somewhere'; %location of the text files
filelist = readtable(resultfile); %load list of file from result file. Creates a table with 5 variables, Var1, Label, Area, XM, and YM
for fileidx = 1:height(filelist) %iterate over the files
[~, filebase] = fileparts(filelist.Label(fileidx)); %get base file name without tif extension
filedata = readtable(fullfile(filefolder, [filebase, '.txt'])); %read text file. Creates a table with 6 variables, Var1, Area, X, Y, XM and YM
filedata{end+1, :} = [height(filedata)+1, filelist.Area(fileidx), Nan, Nan, filelist.XM(fileidx), filelist.YM(fileidx)]; %append one row with the content of the matching entry in filelist
writetable(filedata, fullfile(filefolder, ['Appended_', filebase, '.txt'])); %write to a new file (or original file once you've tested the code).
end

Catégories

En savoir plus sur Tables dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by