How do I dynamically remove specific rows from an excel sheet?
古いコメントを表示
I am currently working on an app which downloads and processes some data from a server. The data is encoded with hashStrings and to keep track of the present files, some specific identifiers of the encoded data are written out into a log file, which is an .xlsx sheet. I want the log file to dynamically update whenever the app is started and remove rows from my log file whever a data file has been removed from the respective folder. my approach was to compare the hash strings in my log files to the hash strings of my .mat (data) files and remove these rows if no match can be found. This does also work pretty well untill i want to overwrite the already existing log file. Whatever i try, i always get the correct "updatedLog" in my workspace but not an actually updated log file in the respective directory. I would be grateful about any insights as to why my approach is not doing what i need it to do.
function updateLogFile(app)
% load the existing log file
logFilePath = app.logFile;
% get he encoded .mat files path
inputDir = fullfile(app.dataFolder, '\Input');
% read log file table and convert entries from "cell" to
% "string" for comparability
currentLogFile = readtable(logFilePath);
currentLogFile.HashStr = string(currentLogFile.HashStr);
% search for the .mat files in question and extract the
% hashstring from the file name. Then put the hashstrings into
% a table for better compatability.
inputFileList = dir(fullfile(inputDir, 'container_*.mat'));
fileHash = cellfun(@(x) regexp(x, '(?<=_)[a-f0-9]+(?=\.mat)', 'match',...
'once'), {inputFileList.name}, 'UniformOutput', false);
fileHash = table(string(fileHash(:)), 'VariableNames', {'HashStr'});
% compare the log file entries with the found file name hash
% strings
hashIsPresent = ismember(currentLogFile.HashStr, fileHash.HashStr);
% only write out the matched rows
updateLog = currentLogFile(hashIsPresent, :);
% write out table "overwrite existing .xlsx file" and recieve the same blooming table i started
% with... >:(
writetable(updateLog, app.logFile);
end
5 件のコメント
Image Analyst
2024 年 12 月 23 日
It would help if you had attached a workbook along with the row numbers that you wish to delete.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
dpb
2024 年 12 月 23 日
hashIsPresent = ismember(currentLogFile.HashStr, fileHash.HashStr);
% only write out the matched rows
updateLog = currentLogFile(hashIsPresent, :);
If you're trying to remove those not present, shouldn't you be keeping the difference between who's in the directory list and the present hash list instead?
Eva
2024 年 12 月 27 日
Eva
2024 年 12 月 27 日
dpb
2024 年 12 月 29 日
It looked to me like there was a good chance the reason the file wasn't changing is because what you were writing was the same as what you read...but, I didn't actually try to debug without any data.
I've never had an instance in which the various versions didn't write what was requested even after reading and munging on the same file so I'm very suspicious it's something else going on.
I was just reminded of the one case in which that might happen is that writetable and friends will NOT accept an absolute Excel address (one with the "$" signs) if the Range parameter is given -- in that case it does just silently fail to write anything. I don't recall if I have previously submitted this as a bug/implementation issue or not but am going to after this last debugging session...
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Spreadsheets についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!