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

7 views (last 30 days)
GJohnson2 on 22 May 2022
Commented: Image Analyst on 23 May 2022
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 Comment
Walter Roberson
Walter Roberson on 22 May 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.

Sign in to comment.

Answers (3)

Image Analyst
Image Analyst on 22 May 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.
baseFileName = 'My Data.csv';
fullFilename = fullfile(folder, baseFileName);
if isfile(fullFileName)
% Delete any existing file with the same name.
writeMatrix(myData, fullFileName);
Waiting for your reply, after you read this link of course:
Image Analyst
Image Analyst on 23 May 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.

Sign in to comment.

GJohnson2 on 23 May 2022
Edited: Walter Roberson on 23 May 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);
writematrix(SubDataVals, rfile_1);
fprintf('Cannot find %s\n',rfile_1);
  1 Comment
Walter Roberson
Walter Roberson on 23 May 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.

Sign in to comment.

Image Analyst
Image Analyst on 23 May 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.




Community Treasure Hunt

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

Start Hunting!

Translated by