Fix write() extreme slowdown for filenames containing underscores#69
Draft
Fix write() extreme slowdown for filenames containing underscores#69
Conversation
- Replace str2num with str2double in make_img_filename to avoid eval-based
path searches that are extremely slow on clusters with large MATLAB paths.
str2double handles non-numeric strings by returning NaN (fast) instead of
triggering a full MATLAB function path search via eval.
- Fix warning('off') to use warning('off', 'all') with proper save/restore
state pattern, fixing deprecated usage in MATLAB R2020a+.
- Fix O(N^2) char concatenation loop in slice write path to use cell array
pre-allocation (O(N)) instead of repeated char() matrix building.
Fixes slow write for filenames like TYPEB_FITHRF.nii where str2num called
with the identifier would trigger expensive MATLAB path lookups.
Co-authored-by: torwager <6262700+torwager@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix slow write speed for specific filenames
Fix write() extreme slowdown for filenames containing underscores
Mar 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Writing NIfTI files via
write()was up to 1000x slower for filenames likeTYPEB_FITHRF.niivsTYPEBFITHRF.nii— after 3 minutes, only 39.7 MB of a 13.9 GB file had been written.Root cause:
str2numinmake_img_filenameusesevalinternally. For non-numeric identifiers,eval('[TYPEB_FITHRF]')triggers a full MATLAB path search. On clusters with large, NFS-mounted MATLAB paths, this lookup is expensive and happens once per volume written.Changes
str2num→str2doubleinmake_img_filename(bothiimg_reconstruct_vols.mandiimg_reconstruct_3dvol.m):str2doublenever invokeseval— non-numeric strings returnNaNinstantly with no path search. Addedisnanfallback to preserve existing behavior.Fix
warning('off')→warning('off', 'all')with save/restore in both files: the barewarning('off')is deprecated since MATLAB R2020a and may silently fail to suppress warnings, causing per-volume warning output during large writes.Fix O(N²)
charconcatenation in slice write path (iimg_reconstruct_vols.m): replaced the growing char array built row-by-row in a loop with a pre-allocated cell array converted viachar()at the end.Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.