When using writetable, I get the error "FILENAME must be a non-empty character vector or string scalar."
19 ビュー (過去 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!