function saveFilename = nappSaveDataTable(DataTable, options) arguments DataTable table options.suffix char = "" options.varName char = "" end currentScriptName = ''; % 1. Determine script name (Your existing logic is solid here) stackInfo = dbstack; if length(stackInfo) > 1 [~, currentScriptName, ~] = fileparts(stackInfo(2).file); if contains(currentScriptName, 'LiveEditorEvaluationHelper') currentScriptName = ''; end end if isempty(currentScriptName) try [~, currentScriptName, ~] = fileparts(matlab.desktop.editor.getActiveFilename); catch currentScriptName = 'ManualRun'; end end % 2. Generate the final filename saveFilename = sprintf('%s%s.mat', currentScriptName, options.suffix); % 3. Determine the variable name % Priority: 1. Manual override, 2. inputname, 3. Default 'DataTable' if options.varName ~= "" finalVarName = options.varName; else finalVarName = inputname(1); if isempty(finalVarName) finalVarName = 'DataTable'; end end % 4. SAVE % We create a struct where the field name is our dynamic string. % Then we save the struct with the '-struct' flag. saveData.(finalVarName) = DataTable; save(saveFilename, '-struct', 'saveData'); fprintf('✅ Successfully saved %s to: %s\n', finalVarName, saveFilename); end