When using writetable, I get the error "FILENAME must be a non-empty character vector or string scalar."
조회 수: 17 (최근 30일)
이전 댓글 표시
What I am trying to do is choose a folder with files in it that contain data, retrieve this data, and save it as a txt file. I am not having any problems with the data itself but rather, naming the text file I am saving the data to. Ideally, I would like to save the text file with the same name as the origional file but I am having trouble doing so. This issue only happens when I am processing more than one file.
I have been working on this issue for a long time and any help would be greatly appreciated.
Folder = uigetdir; % asks user for a folder
FilePattern = fullfile(Folder,'*deer*.dta'); % searches folder for .DTA files with DEER in the name
Files = dir(FilePattern); % puts files into struct array
for n=1:numel(Files)
File = struct2table(Files); % Takes the file name out of a struct and into a table
writetable(Deer_Trace_data{n},[ File{n,1} '.txt']); % names and saves file
end
Small point but currently, it also saves the filename including its previous extension which is .dta. Ideally, this would be removed but its not a big deal.
Please let me know if you need any more information.
Thanks!
댓글 수: 0
채택된 답변
Image Analyst
2021년 12월 16일
Try this:
Folder = uigetdir; % asks user for a folder
FilePattern = fullfile(Folder,'*deer*.dta'); % searches folder for .DTA files with DEER in the name
%filePattern = fullfile(Folder, '*deer*.txt'); % searches folder for .txt files with DEER in the name
Files = dir(filePattern); % puts files into struct array
for n = 1 : numel(Files)
% Get input filename.
fullFileName = fullfile(Files(n).folder, Files(n).name);
fprintf('Reading "%s".\n', fullFileName);
% Call your function to read the .dta file and put it into a table variable.
thisTable = ReadDTAFile(fullFileName);
Deer_Trace_data{n} = thisTable;
% Get output filename. It's the same except the extension is txt instead of dta.
outputFullFileName = strrep(fullFileName, '.dta', '.txt')
% Write the table to a text file.
fprintf('Writing "%s".\n', outputFullFileName);
writetable(Deer_Trace_data{n}, outputFullFileName);
end
댓글 수: 3
Image Analyst
2021년 12월 17일
Looks like my code, except for having the comments removed and prepending "_DEER_Trace" before the output filename. Generally I (and I think most people) would not recommend removing explanatory comments. Thanks for Accepting the answer.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 String Parsing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!