MATLAB Answers

Appending a csv file with existing data already in another column

65 views (last 30 days)
The purpose of the script I am writing is to weigh a number of subjects and record their mass, and then using my script and an image of the subject record their area.
I am trying to figure out if there is a way to write to an existing csv file (which has a column of masses already) and append it on the/an empty column. When I try this with csvwrite it overwrites the entire file.
Using dlmwrite as shown takes the mass column, puts it all in column A and then appends the end of the mass data
dlmwrite('filepath',areas,'roffset',0,'coffset',0,'delimiter',',','-append');
writematrix does the same thing
writematrix(areas,'filepath','WriteMode','append');
Ideally I want the csv to look like this and add data to the first column as it goes through every picture in the folder as per my script
1 area1 mass1
2 area2 mass2
3 area3 mass3
...
100 area100 mass100
right now all I get is
1 mass1
2 mass2
3 mass3
...
49 mass49
50 area1
....
100 area50
Here is the full script for reference purposes
format long g
myFolder = 'youfilepath';
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.jpg'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles) %goes through all files in the folder
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
I = imread(fullFileName); %reads in image files
J = imcrop(I,[375,100,3800,2900]); %crop to subregion of 12 grid
%BW = im2bw(J,0.375); %this is automatic threshold
%BW2 = imcomplement(BW);
%BW3 = imfill(BW2,'holes');
imgcell=cell(1,12);
p = 1;
x = 950;
for i = 1:3 %iterates through 3 rows
y = 965*(i-1);
for j = 1:4 %iteraties through the 4 columns
imgcell{p} = imcrop(J,[x*(j-1),y,950,965]); %crops each grid cell
p = p + 1;
end
end
BW = cell(1,12); %preallocating cell arrays
stats = cell(1,12);
for i =1:12
BW{i} = imgcell{i}(:,:,1)<95; %thresholding each gridded image
BW{i} = imfill(BW{i},4,'holes'); %fills holes (very useful)
stats{i} = regionprops(BW{i},'area'); %collecting pixel area
end
areas = zeros(1,12);
for i = 1:12
areas(i) = stats{i}.Area; %takes area field from stats cell array/structure and puts it into (more familiar) matrix form
end
cutoff = 2e-5; %cutoff area for a brand
scale = 2.620670848e-8; %scale in m^2/pixel
areas = areas*scale; %scaling pixel counts
areas(areas<cutoff) = []; %taking out any values that dont meet the cutoff
areas = transpose(areas); %1x12 isnt as nice as 12x1 in a csv
writematrix(areas,'filepath\myfile.csv','WriteMode','append');
end

  0 Comments

Sign in to comment.

Accepted Answer

Anmol Dhiman
Anmol Dhiman on 20 Jul 2020
Hi Mahdi,
In my opinion, you can read the data first, modify it and then save the data back to the file.
Assuming you have csv file named testfile.csv (having column for masses already):
masses = [1,2,3,4];
masses = masses';
writematrix(masses,'testfile.csv');
% Assuming you have file as above
M = csvread('testfile.csv');
% creating the areas column
areas = [10,20,30,40];
areas = areas';
% to make areas appear in first column
M = [areas M];
% save back to file
writematrix(M,'testfile.csv');
Regards,
Anmol Dhiman

More Answers (1)

Walter Roberson
Walter Roberson on 20 Jul 2020
csvwrite() and dlmwrite() never add columns to existing files.
writematrix() and writetable() and writecell() can only add columns to .xls, .xlsx, .xlsb, .xlsm, .xltx, .xltm files, never to .csv files.
Your choices are:
  1. Use existing tools such as readmatrix() or readtable() to read in all of the existing data, add to the data in memory, and use one of the tools such as writematrix() or writetable() to write out the changed version; or
  2. Use lower-level routines such as fopen() the existing .csv for reading, and fopen a new (**not** the same!) .csv file for writing, and loop doing fgetl() and create a new line with the new data appeneded, and fprintf() it to the new file. Afterwards, fclose both, move the existing file to a backup name (in case the operation broke the file), and rename the new file to the old name.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!