How can I force a .txt export
Ältere Kommentare anzeigen
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 Kommentar
Catalytic
am 8 Mär. 2026
We cannot run your code to see what the problem is. No input to the function is provided.
Akzeptierte Antwort
Weitere Antworten (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 Kommentar
Paul-William
am 8 Mär. 2026
Paul-William
am 9 Mär. 2026
Verschoben: Matt J
am 9 Mär. 2026
Kategorien
Mehr zu Data Import and Analysis finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
