How do I check if two files are different programmatically?

37 ビュー (過去 30 日間)
Monika Jaskolka
Monika Jaskolka 2021 年 10 月 21 日
コメント済み: dpb 2021 年 10 月 21 日
Given two files of the same type (e.g., .m, .xlsx, .fig), I'd like to compare them to check if they are the same or not. How can I programmatically call Matlab's comparison functionality and have it return the status of the comparison?
The use case here is that I have a Development folder and a Release folder. I want to check if there has been changes to the Development files in comparison to the Release version, so I know whether or not it needs to be updated.
  1 件のコメント
dpb
dpb 2021 年 10 月 21 日
MATLAB doesn't have such a builtin comparison functionality for totally general files. You would have several alternatives depending upon how much in depth you want to go --
  1. Check OS timestamps -- if date/time of file in Development folder is later than that of Release, presume modifications. That's what backup routines and version control systems do. This is doable with MATLAB by use of the dir function.
  2. Check actual file content, not just file system date stamp. This would find cases in which the content actually hasn't changed, but the file was opened/touched in such a manner as to have modified the datestamp even if the content didn't actually get modified. There are OS commands to make file comparisons (FC is packaged with the CMD interpreter in Windows), in general it's probably best to just pass the task off to one of those from MATLAB.
  3. "Roll your own" -- not recommended.

サインインしてコメントする。

採用された回答

Monika Jaskolka
Monika Jaskolka 2021 年 10 月 21 日
I ended up using the diff command via system:
[~, result] = system(['diff ''', file1, ''' ''', file2, '''']);
When the result is empty, the files are identical. Otherwise they are different.
  1 件のコメント
dpb
dpb 2021 年 10 月 21 日
My preferred suggestion... :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Find more on Environment and Settings in Help Center and File Exchange

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by