writetable - wrong number of arguments
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Malte Räuchle
am 8 Jul. 2020
Kommentiert: Malte Räuchle
am 8 Jul. 2020
Hello everyone, I'm trying to create a table and export it to Excel. I want to initialize a Result Path before to have a better overview. Is there a trick to insert that?
Result_Path = '..\Result';
writetable(Table_Merged_Max, Result_Path,'Extraction_Mean_Time1.xlsx','Sheet','Maximum');
Error using writetable - Wrong number of arguments.
Appreciate your advice and help!
0 Kommentare
Akzeptierte Antwort
Weitere Antworten (1)
Walter Roberson
am 8 Jul. 2020
Bearbeitet: Walter Roberson
am 8 Jul. 2020
The following code will atempt to debug why your file cannot be written to. In its current form, it is specific to MS WIndows (just because of the drive name handling; the rest is shared.)
If you set create_missing to true then it will create any missing directory along the way instead of just reporting what is missing.
Note: does not handle UNC paths.
Code has not been tested in its present form (an older version of the code was tested.)
Result_Path = '..\Result';
filename = 'Extraction_Mean_Time1.xlsx';
create_missing = false;
dest_dir = Result_Path;
dest_file = filename;
failed = false;
try
%pwd returns the previous directory
curdir = pwd(dest_dir);
dest_fullpath = pwd(curdir); %and go back, returning full name of where we were
catch
fprint('Could not cd to destination directory "%s"\n', dest_dir);
failed = true;
end
if ~failed
if ~ismember(upper(dest_fullpath(1)), 'A':'Z') || dest_fullpath(2) ~= ':'
fprintf('destination does not have a recognizable drive name');
failed = true;
end
end
if ~failed
parts = [dest_fullpath(1:2), regexp(dest_fullpath(3:end), '[/\\]', 'split')];
plen = cellfun(@length, parts);
parts(plen == 0) = []; %zap extra directory separators
if ~exist(parts{1}, 'dir')
fprintf('You do not have a "%s" drive. Giving up.\n', parts{1});
failed = true;
end
end
if ~failed
for K = 2 : length(parts)
thispath = fullfile(parts{1:K});
if exist(thispath, 'dir')
fprintf('Okay we already have directory "%s"\n', thispath);
elseif create_missing
try %#ok<UNRCH>
mkdir(thispath);
fprintf('Was able to create missing directory "%s"\n', thispath);
catch
fprintf('Failed trying to create missing directory "%s"', thispath);
oldpath = fullfile(parts{1:K-1});
[success, p] = fileattrib(oldpath);
if success
fprintf('Attributes of parent directory "%s" are:\n', oldpath);
disp(p);
else
fprintf('Could not fetch attributes of parent directory "%s"]\n', oldpath)
failed = true;
break
end
end
else
fprintf('Directory "%s" does not exist and you asked that missing directories not be created. Set the variable create_missing to true if you want the directory created\n', thispath);
failed = true;
break
end
end
if failed
fprintf('Some directory not created. Not ready to use\n');
else
fprintf('Okay, should be ready to use directory "%s"\n', fullfile(parts{:}) );
end
end
fullpath = fullfile(parts{:}, dest_file);
if ~failed
if ~exist(fullpath, 'file')
[fid, msg] = fopen(fullpath, 'a');
if fid < 0
fprintf('File "%s" did not already exist and cannot be written to because: "%s"\n', fullpath, msg);
failed = true;
else
fprintf('Destination file did not exist but is writable! File "%s"\n', fullpath);
delete(fullpath);
end
else
[success,attrs] = fileattrib(fullpath);
if ~success
fprintf('Somehow, file exists but we cannot ask about its attributes! File "%s"\n', fullpath)
failed = true;
else
if attrs.UserWrite
fprintf('Destination file exists and is writable! File: "%s"\n', fullpath)
else
fprintf('Destination file exists but cannot be written to! File: "%s"\n', fullpath)
failed = true;
end
end
end
end
if ~failed
fprintf('Okay, should be ready to use file "%s"\n', fullpath);
end
Siehe auch
Kategorien
Mehr zu Search Path finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!