identifying if a number stays the same between files
2 次查看(过去 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
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?
回答(1 个)
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 个评论
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 Center 和 File Exchange 中查找有关 Spectral Measurements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!