How can I force a .txt export

Hi,
I am working on a program that has to export .txt files, the problem is these files get named in versions as in V3.02 and then the files get saved as .02 files instead of .txt if I dont write it out every time.
is there a way to force the .txt ending?
heres my export function:
%% export
function exportSelectedCurve(fig1)
H = guidata(fig1);
p = H.params;
fs = H.fs;
t = H.t;
% --- Filter vorbereiten
[b,a] = butter(4, p.fc/(fs/2));
MA = @(x) movmean(x,p.win);
SG = @(x) sgolayfilt(x,p.order,p.framelen);
BW = @(x) filtfilt(b,a,x);
filters = {@(x)x, MA, SG, BW};
filt = filters{H.exportCurve};
% --- Daten filtern
HipZ_L = filt(H.HipZ_L + H.offset(3));
HipX_L = filt(H.HipX_L + H.offset(1));
KneeX_L = filt(H.KneeX_L + H.offset(5));
HipZ_R = filt(H.HipZ_R + H.offset(4));
HipX_R = filt(H.HipX_R + H.offset(2));
KneeX_R = filt(H.KneeX_R + H.offset(6));
% --- Outputmatrix
out = [ ...
t, ...
HipZ_L, HipX_L, KneeX_L, zeros(size(t)), ...
HipZ_R, HipX_R, KneeX_R, zeros(size(t)) ];
header = ['time,left_hip_abduction_joint,left_hip_extension_joint,' ...
'left_knee_joint,left_ankle_joint,' ...
'right_hip_abduction_joint,right_hip_extension_joint,' ...
'right_knee_joint,right_ankle_joint'];
defaultName = [H.importName '_smoothed.txt'];
% --- Save-Dialog
[file, path] = uiputfile( ...
{'*.txt','Text files (*.txt)'}, ...
'Export speichern unter', ...
defaultName);
if isequal(file,0) || isequal(path,0)
return;
end
% --- Nur .txt ergänzen, nichts abschneiden
if length(file) < 4 || ~strcmpi(file(max(1,end-3):end), '.txt')
file = [file '.txt'];
end
fname = fullfile(path, file);
% --- Datei öffnen
fid = fopen(fname, 'w');
if fid == -1
errordlg(['Datei konnte nicht geöffnet werden: ' fname], 'Exportfehler');
return;
end
% --- Header schreiben
fprintf(fid, '%s\n', header);
fclose(fid);
% --- Daten anhängen
writematrix(out, fname, 'Delimiter', ',', 'WriteMode', 'append');
disp(['Exported: ' fname]);
end

1 Commento

We cannot run your code to see what the problem is. No input to the function is provided.

Accedi per commentare.

 Risposta accettata

First of all NEVER overwrite the built in path variable. It's a reserved variable name and has information in there that you do not want to destroy. So do not do this:
[file, path] = uiputfile( ...
do this instead
[baseFileNameWithExtension, folder] = uiputfile( ...
Notice how I use "folder" instead of the reserved "path". Or you can use "location" like it suggests in the help for uiputfile.
Now to force a "txt" file extension you can get the base filename without an extension and then just create the full filename with folder and txt entension
[~, baseFileNameNoExt, ext] = fileparts(baseFileNameWithExtension);
% Now will ignore the actual ext and create a full filename with the txt extension that we want.
fullFileName = fullfile(folder, baseFileNameNoExt, '.txt');

7 Commenti

ok thanks I changed out path as you said. implemented your fix but it doesnt change the outcome, still get a .0X file insted of a .txt file.
thats what I wrote, is that what you meant?
% --- Save-Dialog
[baseFileNameWithExtension, folder] = uiputfile( ...
{'*.txt','Text files (*.txt)'}, ...
'Export speichern unter', ...
defaultName);
if isequal(baseFileNameWithExtension,0) || isequal(folder,0)
return;
end
% --- always .txt export
[~, baseFileNameNoExt, ~] = fileparts(baseFileNameWithExtension);
fname = fullfile(folder, [baseFileNameNoExt '.txt']);
Image Analyst
Image Analyst il 8 Mar 2026
Modificato: Image Analyst il 8 Mar 2026
Yes, that should work. Take off the semicolons and see what the values of the variables are. But fname is very ambigous as to exactly what it is. Is if the full file name with folder and extension? Or just the base file name with extension but no path. That's why I prefer explicit variable names like I used.
No it didnt work, switched to my linux laptop, here atleast it shows the .txt when opening the fileexplorer so I get remindet, that I have to write .txt at the end. Windows cut the ending when exporting.
I ran your code on my Windows computer
defaultName = fullfile(pwd, 'paul.txt')
% --- Save-Dialog
[baseFileNameWithExtension, folder] = uiputfile( ...
{'*.txt','Text files (*.txt)'}, ...
'Export speichern unter', ...
defaultName);
if isequal(baseFileNameWithExtension,0) || isequal(folder,0)
return;
end
% --- always .txt export
[~, baseFileNameNoExt, ~] = fileparts(baseFileNameWithExtension)
fname = fullfile(folder, [baseFileNameNoExt '.txt'])
and it worked as seen by this stuff in the command window:
defaultName =
'C:\Users\haywo\OneDrive\Documents\MATLAB\work\Tests\paul.txt'
baseFileNameNoExt =
'paul'
fname =
'C:\Users\haywo\OneDrive\Documents\MATLAB\work\Tests\paul.txt'
I even typed in a file with no extension, and it added the .txt correctly as shown above. I still think you should answer my request to remove semicolons and tell us what it says, just like I did above.
sooo, I finally figured out, that there are two different file export functions in the code (group project, I am sorry), in different matlab files, turns out your solution works as far as forcing the .txt ending.
Only problem now is that it cuts at the "." so a V3.08 will export as a V3.txt
ok I figured that second problem out myself, thank you :)

Accedi per commentare.

Più risposte (2)

Matt J
Matt J il 8 Mar 2026
Modificato: Matt J il 8 Mar 2026
Instead of,
if length(file) < 4 || ~strcmpi(file(max(1,end-3):end), '.txt')
file = [file '.txt'];
end
use fileparts to detect the extension,
[~,~,extension]=fileparts(file);
if ~strcmp(extension,'.txt')
file=strcat(file,'.txt');
end
Paul-William
Paul-William il 9 Mar 2026
Spostato: Matt J il 9 Mar 2026
here the final version for someone looking this up later:
function cbExport(~,~)
Sin = guidata(fig);
thisFile = mfilename('fullpath');
basePath = fileparts(thisFile);
outDir = fullfile(basePath, 'trajektorien');
if ~exist(outDir,'dir')
mkdir(outDir);
end
T = Sin.Torig * Sin.time.scale;
ttOut = linspace(Sin.t1, Sin.t1 + T, round(T*100) + 1).';
sOut = (ttOut - Sin.t1) / T;
Y100 = cell(1,6);
for j = 1:6
yj = Sin.cur{j};
yOut = interp1(Sin.sBase, yj, sOut, 'linear');
Y100{j} = deg2rad(yOut);
end
out = [ttOut(:), ...
Y100{3}(:), Y100{1}(:), Y100{5}(:), zeros(numel(ttOut),1), ...
Y100{4}(:), Y100{2}(:), Y100{6}(:), zeros(numel(ttOut),1)];
header = ['time,left_hip_abduction_joint,left_hip_extension_joint,' ...
'left_knee_joint,left_ankle_joint,' ...
'right_hip_abduction_joint,right_hip_extension_joint,' ...
'right_knee_joint,right_ankle_joint'];
defaultName = 'trajectory.txt';
% --- Save-Dialog
[userFileName, folder] = uiputfile( ...
{'*.txt','Text files (*.txt)'}, ...
'Save export as', ...
fullfile(outDir, defaultName));
if isequal(userFileName,0) || isequal(folder,0)
return;
end
% --- force .txt but dont cut
if ~endsWith(lower(userFileName), '.txt')
userFileName = [userFileName '.txt'];
end
fullName = fullfile(folder, userFileName);
fid = fopen(fullName, 'w');
if fid == -1
errordlg(['Datei konnte nicht geöffnet werden: ' fullName], 'Exportfehler');
return;
end
fprintf(fid, '%s\n', header);
fclose(fid);
writematrix(out, fullName, 'Delimiter', ',', 'WriteMode', 'append');
disp(['Exported to: ' fullName]);
end

Categorie

Scopri di più su Data Import and Analysis in Centro assistenza e File Exchange

Prodotti

Release

R2025b

Community Treasure Hunt

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

Start Hunting!

Translated by