writematrix is not creating a new .csv file for writing matrix to if it does not already exist.

28 vues (au cours des 30 derniers jours)
Info sheet for writematrix function says that it will create a new file with the 'full filename path' if it does not already exits. This is not happening and I am getting a 'Cannot find 'full filename path' instead.
  1 commentaire
Walter Roberson
Walter Roberson le 22 Mai 2022
Modifié(e) : Walter Roberson le 22 Mai 2022
Are you possibly using the "WriteMode", "append" option?
If you are passing in a path with directory, does the directory already exist? The function will not create directories.

Connectez-vous pour commenter.

Réponses (3)

Image Analyst
Image Analyst le 22 Mai 2022
Show the code where you're creating the folder name and the full file name. You seem to have forgotten to upload it. It should look something like
folder = 'c:\whatever'
if ~isfolder(folder)
% Create folder if it doesn't exist. Maybe writematrix does this for you
% -- I'm not sure because you're saying it doesn't, so I'm doing it manually here.
mkdir(folder)
end
baseFileName = 'My Data.csv';
fullFilename = fullfile(folder, baseFileName);
if isfile(fullFileName)
% Delete any existing file with the same name.
delete(fullFileName);
end
writeMatrix(myData, fullFileName);
Waiting for your reply, after you read this link of course:
  2 commentaires
Amina Alihodzic
Amina Alihodzic le 23 Mai 2022
@Image Analyst is it possible to get yout e-mail :) I have a few questions for you regarding steganography..
Image Analyst
Image Analyst le 23 Mai 2022
Sorry, no. Ask them in Answers where everyone can benefit from the topic. I am not an expert in steganography so I'll probably just say "I don't know" to whatever you ask. I'm attaching a few demos.

Connectez-vous pour commenter.


GJohnson2
GJohnson2 le 23 Mai 2022
Modifié(e) : Walter Roberson le 23 Mai 2022
It looks like the writematrix function is not completed successfully, and throws a catch error.
The code is:
rfile_1 = sprintf('/Documents/Matlab/data/S%d/Exp%d/S%d_Exp%d_RIGHT_%s.csv', snum, exp_num, snum, exp_num, date_str);
try
writematrix(SubDataVals, rfile_1);
catch
fprintf('Cannot find %s\n',rfile_1);
end
  1 commentaire
Walter Roberson
Walter Roberson le 23 Mai 2022
Which operating system are you using that has "Documents" directly under the root directory ?
"Documents" is much more likely to be under the user's home directory, such as /Users/GJ2/Documents/Matlab/data/ and so on.
I do not see anywhere in your code where you are creating any directory Documents/Matlab/data/S%d/Exp%d that does not already exist.

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 23 Mai 2022
If you're doing something like
date_str = datestr(now)
date_str = '23-May-2022 12:49:11'
that won't work because it has colons in the string and you can't have colons in the filename anywhere except in the second position to separate the drive letter from the folder. To fix, get rid of the colons:
rfile_1 = strrep(rfile_1, ':', '') % Get rid of colons.

Catégories

En savoir plus sur Data Import and Export dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by