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)
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
Error using matlab.internal.lang.capability.Capability.require (line 94)
Support for Java user interfaces is required, which is not available on this platform.

Error in uigetdir (line 52)
Capability.require(Capability.Swing);
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!

Réponse acceptée

Image Analyst
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
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.
Alex Garces
Alex Garces le 17 Déc 2021
You are correct that is my fault. The issue was with outputFullFileName in strrep being changed to Files(n).name, that way the entire file path isn't used to name the file. I will include explanatory comments in the future.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by