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';
filePattern = fullfile(myFolder, '*.jpg');
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
I = imread(fullFileName);
J = imcrop(I,[375,100,3800,2900]);
imgcell=cell(1,12);
p = 1;
x = 950;
for i = 1:3
y = 965*(i-1);
for j = 1:4
imgcell{p} = imcrop(J,[x*(j-1),y,950,965]);
p = p + 1;
end
end
BW = cell(1,12);
stats = cell(1,12);
for i =1:12
BW{i} = imgcell{i}(:,:,1)<95;
BW{i} = imfill(BW{i},4,'holes');
stats{i} = regionprops(BW{i},'area');
end
areas = zeros(1,12);
for i = 1:12
areas(i) = stats{i}.Area;
end
cutoff = 2e-5;
scale = 2.620670848e-8;
areas = areas*scale;
areas(areas<cutoff) = [];
areas = transpose(areas);
writematrix(areas,'filepath\myfile.csv','WriteMode','append');
end
0 Comments
Sign in to comment.