MATLAB Answers

Editing CSV File Specifying Range for Mac User

2 views (last 30 days)
Carlos on 10 Feb 2016
Commented: Carlos on 28 Feb 2016
I'm wondering if anyone has been able to write to a csv file specifying a range to write to.
Xlswrite worked perfectly when I ran my program on windows, but it's a different story for mac.
I have been able to append to csv files using the following code:
function cell2csv(filename,cellArray,delimiter,mode)
% Writes cell array content into a *.csv file.
% CELL2CSV(filename,cellArray,delimiter)
% filename = Name of the file to save. [ i.e. 'text.csv' ]
% cellarray = Name of the Cell Array where the data is in
% delimiter = seperating sign, normally:',' (it's default)
% mode = specifies the mode of opening the file. See fopen() for a detailed
% list (default is overwrite i.e. 'w')
% by Sylvain Fiedler, KA, 2004
% modified by Rob Kohr, Rutgers, 2005 - changed to english and fixed delimiter
if nargin<3
delimiter = ',';
if nargin<4
mode = 'w';
datei = fopen(filename,mode);
for z=1:size(cellArray,1)
for s=1:size(cellArray,2)
var = eval(['cellArray{z,s}']);
if size(var,1) == 0
var = '';
if isnumeric(var) == 1
var = num2str(var);
if s ~= size(cellArray,2)
However I must update a certain column in my csv data.
Anyone know how i could possibly tweak this code or CELL2CSV to include a range to edit my csv file?
Thanks in advance


Jan on 10 Feb 2016
Please use the "{} Code" button, to make the code readable. And omit the ugly and evil and useless eval().
What exactly does "but it's a different story for mac" mean? This detail matters and I cannot guess it.
Walter Roberson
Walter Roberson on 10 Feb 2016
In MS Windows that has Excel installed, MATLAB uses ActiveX to talk to Excel to do xlsread() and xlswrite(), so MATLAB is able to access the full flexibility of Excel. If Excel is not installed or the operating system is not MS Windows then this path does not work, so MATLAB is restricted to some code that handles a subset of possibilities. One of the restrictions is that ranges cannot be specified.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 10 Feb 2016
Edited: Walter Roberson on 10 Feb 2016
If you are not using MS Windows with Excel installed, then you have to use the standard mechanisms to replace a portion of a file:
Open the input file. Open a (different) output file. Read lines from the input file and write them to the output file until you reach a line that needs to be changed. Read the old version of the line from the input file, replace portions of the content with the desired new data, and write the result to the output file. Keep doing that until you are finished changing lines. Then go back to reading input lines and copying them to the output file. At the end, close both files, rename the input file to a backup name, rename the output file to the input file name. If you are really confident in the change than then you can delete the renamed input file. At every step of this process you need to ensure that if you interrupted or the system rebooted or had a glitch or that there was a bug in your code, that you always have a known-good version of the file available to go back to.
This process of copying from input file to output file is strictly necessary for text files in which any output text might not be exactly the same size as the input text. This is a fundamental restriction due to how text files are implemented on pretty much all operating systems since the early 1980's. If the replacement is exactly the same size as the original, then whether the file is binary or not, there is operating system support, but using the two file process is a good idea.
With the facilities provided by MATLAB, this procedure is also necessary if the output file will be shorter at all than the input file (e.g., if lines got deleted); that part is an inexplicable restriction in MATLAB as all operating systems since the 1960's have provided mechanisms to truncate files.

  1 Comment

Carlos on 28 Feb 2016
Thanks for the detailed explanation Walter. This is very sad since I am basically creating a database and the two file process would be extremely inefficient as my file will exponentially get larger and larger. I will try to look into an alternatively as this is something my program really needs. Thank you!

Sign in to comment.

More Answers (0)