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
Catalytic
2026년 3월 8일
We cannot run your code to see what the problem is. No input to the function is provided.
채택된 답변
추가 답변 (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
Paul-William
2026년 3월 9일
이동: Matt J
2026년 3월 9일
카테고리
도움말 센터 및 File Exchange에서 Data Import and Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
