How can I append table to csv file?

13 Ansichten (letzte 30 Tage)
Okan DÜZYEL
Okan DÜZYEL am 2 Jun. 2022
Kommentiert: melanie becker am 24 Aug. 2023
Hi,
I am dealing with appending problem with csv file. Briefly, I have images those come from with for loop. Every iteration I find something in image and create a new table that contains string and numerical data, but I can't merge it to csv file for every iteration. How can I do it?
Getting that error:
Undefined function 'real' for input arguments of type 'table'.
Error in dlmwrite (line 163)
str = sprintf('%.*g%+.*gi',precn,real(m(i,j)),precn,imag(m(i,j)));
Error in challenge (line 84)
dlmwrite('matrix.csv',T,'delimiter',',','-append');
My code like this:
folder = 'Downloads/challenge/';
filePattern = fullfile(folder, '*.png');
srcFiles = dir(filePattern)
numFiles = length(srcFiles)
if numFiles == 0
message = sprintf('There are no png files are in folder:\n%s', folder);
uiwait(warndlg(message));
else
fprintf('There are %d files in %s:\n', numFiles, folder);
for k = 1 : numFiles
fprintf(' %s\n', srcFiles(k).name);
image = uint8(imread(srcFiles(k).name));
[T,EM] = graythresh(image);
BW = imbinarize(image,0.3);
[Ctrs,Radi] = imfindcircles(BW(:,:,1), [11 20],'ObjectPolarity','dark', 'Sensitivity',0.925);
h = viscircles(Ctrs,Radi);
T = array2table(Ctrs);
S = srcFiles(k).name(1:end-4);
T{:,'NewColumn'} = S;
dlmwrite('matrix.csv',T,'delimiter',',','-append');
clear T;
clear fid;
clear Radi;
clear Ctrs;
clear image;
clearvars -global
end
end

Antworten (1)

MJFcoNaN
MJFcoNaN am 2 Jun. 2022
Hello,
dlmwrite cannot deal with table, but take numetric matrix or cell, such as [1 2 3] or {1,2,3}.
You may take the values of T directly, for example
dlmwrite('matrix.csv',T{:,:},'delimiter',',','-append');
PS: Since R2019a, "dlmwrite is not recommended. Use "writematrix" instead."
PS2: You may be interested in a function of "writetable" as well.
  2 Kommentare
Walter Roberson
Walter Roberson am 2 Jun. 2022
T contains multiple data types, you would not be able to dlmwrite T{:,:}
Use writetable. New enough versions of MATLAB support 'WriteMode', 'append' which makes the task much easier.
melanie becker
melanie becker am 24 Aug. 2023
Hello, I'd a code that worked for matlab2022beta:
writetable(T,'outfile.txt','Delimiter',' ', 'WriteMode','append')
writetable(T,'outfile.csv','WriteMode','append')
... but now using matlab2017a I cannot use it anymore (writetable does not know append for writemode).
How I can write my test file with matlab2017a?
I use dlmwrite but I've this error: "Undefined function 'real' for input arguments of type 'string'"
My table contains both string & real,... How I can write both of them?
THanks

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by