using fullfile when you need to pass a string as an input argument with readable TreatAsEmpty

15 次查看(过去 30 天)
Hi,
I have a folder of files which have strings as headers and when there are fields which the measuring system could not measure it fills those fields with a string ('UND. -62011'), I have been using
Folder_used = dir('/Users/username/Desktop/data/*.txt');
files = Folder_used;
for k=1:numel(files)
data=readtable(files(k).name,'FileType','text',...
'Delimiter','tab','TreatAsEmpty',["UND. -60001","UND. -2011","UND. -62011"]);
%do stuff
end
which works fine. When I want to put this in a standalone app I have a problem. I need to use fullfile to pass the location of the directory and then have the user select the folder, then combine them
i assign the directory as A1 having after having converted it to a string
for k=1:numel(files)
full_name=fullfile(A1,files(k).name)
data=readtable(full_name,'FileType','text',...
'Delimiter','tab',...
'TreatAsEmpty',{'UND. -60001','UND. -2011','UND. -62011'});
but get the error
*Error using fullfile (line 51) String input not supported.
Error in tool/rows3d_Callback (line 157) full_name=fullfile(A1,files(k).name)
Error while evaluating UIControl Callback.*||||
Is there a way to use/get around the read table TreatAsEmpty argument with a string input and use fullfile?
Best regards,
Steve

采纳的回答

Jan
Jan 2018-2-5
编辑:Jan 2018-2-5
The problem is here:
i assign the directory as A1 having after having converted it to a string
You did not provide the code you use for this. But the error message is clear: fullfile requires a char vector, not a modern string object.
Folder = '/Users/username/Desktop/data/'; % As char vector, not as string
FileList = dir(fullfile(Folder, '*.txt'));
for k = 1:numel(FileList)
File = fullfile(Folder, files(k).name);
data = readtable(File, 'FileType', 'text', ...
'Delimiter','tab','TreatAsEmpty', ...
{'UND. -60001', 'UND. -2011', 'UND. -62011'});
%do stuff
end
In modern Matlab version the folder is replied by dir also. Then this works also:
File = fullfile(files(k).folder, files(k).name);
Is there a way to use/get around the read table TreatAsEmpty argument
with a string input and use fullfile?
Neither readtable nor the 'TreatAsEmpty' argument has been the problem, but exactly as the error message told you, only the input argument of fullfile.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by