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

28 visualizaciones (últimos 30 días)
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 comentario
Walter Roberson
Walter Roberson el 22 de Mayo de 2022
Editada: Walter Roberson el 22 de Mayo de 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.

Iniciar sesión para comentar.

Respuestas (3)

Image Analyst
Image Analyst el 22 de Mayo de 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 comentarios
Amina Alihodzic
Amina Alihodzic el 23 de Mayo de 2022
@Image Analyst is it possible to get yout e-mail :) I have a few questions for you regarding steganography..
Image Analyst
Image Analyst el 23 de Mayo de 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.

Iniciar sesión para comentar.


GJohnson2
GJohnson2 el 23 de Mayo de 2022
Editada: Walter Roberson el 23 de Mayo de 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 comentario
Walter Roberson
Walter Roberson el 23 de Mayo de 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.

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 23 de Mayo de 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.

Categorías

Más información sobre Data Import and Export en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by