identifying if a number stays the same between files
2 views (last 30 days)
Show older comments
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 Comments
Rik
on 18 Oct 2021
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?
Answers (1)
Johannes Hougaard
on 18 Oct 2021
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 Comments
Johannes Hougaard
on 19 Oct 2021
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
See Also
Categories
Find more on Spectral Measurements 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!