How to Export and append in a csv file?
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi everyone,
I have a scrip where I have few list which I use to control some input and do some calculation. At the end of the process I have always something like
Vector_1={'Solar in', 0, 0, 0, 10; 'Solar Out',0,0,0,0,5}; Vector_2={'Boiler in', 0, 0, 0, 10; 'Boiler Out',0,0,0,0,5};
After obtain the two lists I create a new list by combine the previous ones. Input=[Vector_1;Vector_2];
Then I want to export and append at each step Input in a csv file doing this:
dlmwrite('Input.csv', Input,'-append')
but I got this error :
Error using dlmwrite (line 118) The input cell array cannot be converted to a matrix.
Thanks for any advice!
0 comentarios
Respuestas (1)
Rajanya
el 22 de Nov. de 2024 a las 12:41
I’ve been able to reproduce the issue on my end and it seems the error you are facing stems from the fact that ‘dlmwrite’ only takes a cell array of numeric values as an input argument; but ‘Input’ has mixed type data.
As a workaround, you could convert ‘Input’ to a table and then use ‘writetable’ in append mode to achieve the same result.
For example, the code could look like:
Vector_1={'Solar in', 0, 0, 0, 10; 'Solar Out',0,0,0,5}; %this had one 0 extra
Vector_2={'Boiler in', 0, 0, 0, 10; 'Boiler Out',0,0,0,5}; %same here
Input=[Vector_1;Vector_2];
T=cell2table(Input); %convert to a table
writetable(T, 'Input_.csv', 'WriteVariableNames', false, 'WriteMode', 'append');
writetable(T, 'Input_.csv', 'WriteVariableNames', false, 'WriteMode', 'append');
This successfully appends ‘T’ at the end of ‘Input_.csv’ file as shown:
If ‘WriteMode’ is not supported for ‘writetable’ in your version of MATLAB, you can append the data manually to the file, following something like this:
if exist('Input_.csv', 'file')
T_existing = readtable('Input_.csv', 'ReadVariableNames', true); %take care of the variable names
% as they should match for proper appending
T_new = [T_existing; T]; % Concatenate the existing and the new data (stored in T)
else
T_new = T; %just the new data (stored in T)
end
writetable(T_new, 'Input_.csv', 'WriteVariableNames', true);
For more information on ‘dlmwrite’ or ‘writetable’, you can visit their respective documentation pages by executing the following commands from the MATLAB Command Window:
doc dlmwrite
doc writetable
Thanks!
1 comentario
Walter Roberson
el 22 de Nov. de 2024 a las 21:59
It would be more natural to use writecell -- other than the fact that writecell was introduced in R2019a and this question was asked in 2015 time-frame.
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!