identifying if a number stays the same between files

1 次查看(过去 30 天)
I have 3 .csv files. I want to know if the numbers in column 5 of the first file stay the same or change between the next 2 files.
Can anybody help me write a simple code to do this?
  2 个评论
Rik
Rik 2021-10-18
Why don't you do it yourself? Try to split this in steps you can solve.
Did you manage to read each file to a variable?
Did you manage to compare values once you have loaded them into Matlab?
C.G.
C.G. 2021-10-18
编辑:C.G. 2021-10-18
I've written a method out and tried to do it but with no success. I cant work out how to compare the values between the files, hence my question.
%% Method to implement:
%identify if the numbers in columns 5 and 6 have changed between files 1 and 2
%if the number is the same, record the particle ID from column 1
% if the number appears in consecutive columns, record how many times the
% number is consectively seen
file = dir('*.csv'); %read the files into matlab
num_files = length(file); %record how many files have been found
[~, index] = natsort({file.name}); %sort the files into proper numerical order (e.g. 1,2,3)
filelist = file(index);
rows = zeros(1, num_files); % create a empty matrix
for a = 1:num_files
T = table2array(readtable(filelist(a).name)); %read in the values
if T(:,5)== T(:,5) & T(:,6) == T(:,6); %see if numbers in col 5& 6 are the same
S = T(ismember(T(:,1),dataset(R,1)),:); % if number is the same, record number in col 1
end

请先登录,再进行评论。

回答(1 个)

Johannes Hougaard
Johannes Hougaard 2021-10-18
I'm not sure if I understand your question correctly - at least if I do then the data you've included are all not identical.
But if the question is "are all data in column 5 the same in ATF_1.csv and ATF_2.csv" then the answer is here.
atf_files = dir("ATF_*.csv");
isthesame = false(size(atf_files));
for ii = 2:length(atf_files)
atf_1 = readmatrix(atf_files(ii-1).name);
atf_2 = readmatrix(atf_files(ii).name);
if all(size(atf_1(:,5)) == size(atf_2(:,5)))
isthesame(ii) = all(atf_1(:,5) == atf_2(:,5));
end
end
  2 个评论
C.G.
C.G. 2021-10-18
编辑:C.G. 2021-10-18
Yes the number of rows in each file can change each time.
I want to know if the individual values in column 5 of ATF_1 change between _1 and 2 and then _2 and _3. From running your code I think it tells me if the whole column changes.
Johannes Hougaard
Johannes Hougaard 2021-10-19
Which individual values do you want to check when the number of rows change?
Do you want to compare row 1 of file ATF_2 to row 1 of file ATF_1? If so you can do
atf_files = dir("ATF_*.csv");
identicalrows = cell(length(atf_files)-1,1);
for ii = 2:length(atf_files)
atf_1 = readmatrix(atf_files(ii-1).name);
atf_2 = readmatrix(atf_files(ii).name);
N = min(size(atf_1,1),size(atf_2,1));
identicalrows{ii-1} = false(N,1);
for jj = 1:N
identicalrows{ii-1}(jj) = atf_2(jj,5) == atf_1(jj,5);
end
end
...and then you do nothing for your last rows;
- or do you want to compare row 1 of file ATF_2 to all rows of ATF_1 and find which one is identical?
atf_files = dir("ATF_*.csv");
identicalrows = cell(length(atf_files)-1,1);
rowindex = cell(length(atf_files)-1,1);
for ii = 2:length(atf_files)
atf_1 = readmatrix(atf_files(ii-1).name);
atf_2 = readmatrix(atf_files(ii).name);
identicalrows{ii-1} = false(size(atf_2,1),1);
rowindex{ii-1} = nan(size(atf_2,1),1);
for jj = 1:size(atf_2,1)
identicalrows{ii-1}(jj) = any(atf_2(jj,5) == atf_1(:,5));
if any(atf_2(jj,5) == atf_1(:,5))
rowindex{ii-1}(jj) = find(atf_2(jj,5) == atf_1(:,5),1);
end
end
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by