How can I force a .txt export
Show older comments
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 Comment
Catalytic
on 8 Mar 2026 at 14:53
We cannot run your code to see what the problem is. No input to the function is provided.
Accepted Answer
More Answers (2)
Instead of,
if length(file) < 4 || ~strcmpi(file(max(1,end-3):end), '.txt')
file = [file '.txt'];
end
[~,~,extension]=fileparts(file);
if ~strcmp(extension,'.txt')
file=strcat(file,'.txt');
end
1 Comment
Paul-William
on 8 Mar 2026 at 15:04
Paul-William
about 2 hours ago
Moved: Matt J
13 minutes ago
Categories
Find more on Data Import and Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!