When using writetable, I get the error "FILENAME must be a non-empty character vector or string scalar."
20 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Alex Garces
le 16 Déc 2021
Commenté : Alex Garces
le 17 Déc 2021
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 commentaires
Réponse acceptée
Image Analyst
le 16 Déc 2021
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 commentaires
Image Analyst
le 17 Déc 2021
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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur String Parsing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!