Editing CSV File Specifying Range for Mac User
6 views (last 30 days)
Show older comments
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 = ',';
end
if nargin<4
mode = 'w';
end
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 = '';
end
if isnumeric(var) == 1
var = num2str(var);
end
fprintf(datei,var);
if s ~= size(cellArray,2)
fprintf(datei,[delimiter]);
end
end
fprintf(datei,'\n');
end
fclose(dabei);
------------
However I must update a certain column in my csv data.
I attempted using xlwrite but this code seems incompatible for csv files: http://www.mathworks.com/matlabcentral/fileexchange/38591-xlwrite--generate-xls-x--files-without-excel-on-mac-linux-win
Anyone know how i could possibly tweak this code or CELL2CSV to include a range to edit my csv file?
Thanks in advance
2 Comments
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.
Accepted Answer
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.
More Answers (0)
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!