MATLAB Answers

Is there a way to return the status of writetable, writematrix, or writecell?

9 views (last 30 days)
Elisa Chen
Elisa Chen on 23 Sep 2020 at 18:43
Edited: Adam Danz on 23 Sep 2020 at 19:34
I've been assigned to maintain some old code from 2014, and the old code uses xlswrite, which is according to MATLAB, outdated, and that makes sense why it's not working. I saw the alternatives to xlswrite are writetable, writematrix, or write cell, but I noticed that it doesn't seem any of those functions support returning the status of the write automatically (1 for successful, 0 otherwise) like xlswrite did.
This is important because the rest of the code depends on this status being true. I know I could just change xlswrite to writetable, then have a separate line where I set status = true, but then it's not actually writing the status and it's not guaranteed that the write was successful. I've seen some other MATLAB support forums that show that writetable is not always successful.
This code is to be compiled into a GUI with neat, clean errors shown to the user so I can't just let the errors from writetable pop up to the end user unexpectedly.
Is there really no alternative to xlswrite that has a built-in status output?

  0 Comments

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 23 Sep 2020 at 19:23
Edited: Adam Danz on 23 Sep 2020 at 19:35
"Is there a way to return the status of writetable, writematrix, or writecell?"
No. But there are workarounds depending on how you'd like to check the status. xlswrite writes the file within a try/catch block and returns false if the try-part throws an error. You could do the same using,
status = true;
try
writetable(___)
catch ME
status = false;
end
You could check if the newly writen file exists using
writetable(T,filename); % where filename is the full path
if exist(filename,'file')==2
status = true;
else
status = false;
end
If you are concerned about specific values being written to the file, you could read in the newly written file and compare the read-in values with the values that were written to the file. However, be aware of precision limits; the numer pi for example won't be written to infinite decimals.
writetable(T,filename)
Tcheck = readtable(filename);
if max(T.Var1 - Tcheck.Var1)<1e-06
status = true;
else
status = false;
end
I don't find any of these to be useful, though. What good is it if the file wasn't written correctly? Instead, I would assign a constant value true to the status variable and would use assert() to throw an error if any of these tests failed.
assert(exist(filename,'file')==2, 'File missing.')
Tcheck = readtable(filename);
assert(max(T.Var1 - Tcheck.Var1)<1e-06, 'Values in file do not match original data.')
" the old code uses xlswrite, which is according to MATLAB, outdated, and that makes sense why it's not working"
As far as I know, current releases of Matlab should be backward compatible with xlswrite, although it's still a good idea to update those functions.

  1 Comment

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by