i have created a code such that data is appended to csv file every time the loop runs
but when i run the program again the new data is added with the old one
but i want to clear out the data in then csvfile before hand
what command do i use
delete('xyz.csv') deletes the whole file

4 comentarios

SINDU GOKULAPATI
SINDU GOKULAPATI el 24 de Mzo. de 2021
i acctually dont want to append
i want to delete the existing data completely
Merve Buyukbas
Merve Buyukbas el 24 de Mzo. de 2021
But you shouldn't count on the main directory and instead use full paths everywhere in your script. This way the data can be located anywhere independently on where the code is.
SINDU GOKULAPATI
SINDU GOKULAPATI el 24 de Mzo. de 2021
im sorry i dont know if im not explaning properly
i have a csv file with all numbers it self ,let that be just 3 cells in my csv with data 1,2,3
if i open csv file i can see those three cells only
now using code in matlab i want to erase those
so now when i open that csv file it should be empty
there is no .txt or sring data
hope my question is conveys right

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 24 de Mzo. de 2021

1 voto

fclose(fopen('xyz.csv', 'w'));
When you open a file for writing with 'w' access, the existing content is discarded.
Note: the POSIX standard library specifies truncate() and ftruncate() functions that would be a closer match. However, MATLAB does not implement either of those.

6 comentarios

SINDU GOKULAPATI
SINDU GOKULAPATI el 30 de Mzo. de 2021
thank you very much walter it worked
SINDU GOKULAPATI
SINDU GOKULAPATI el 11 de Mayo de 2021
hello walter
after using that particular code im not able to perform csvread operation so is there any alternative?
Walter Roberson
Walter Roberson el 11 de Mayo de 2021
? The entire purpose of this code is to "clear out the data in then csvfile before hand" . After using it, the file is going to be empty (provided that you had write access to the file.)
SINDU GOKULAPATI
SINDU GOKULAPATI el 12 de Mayo de 2021
dlmwrite('on.csv',(matrix),'-append')
s=csvread('D:\sem6\PROJECT\on.csv',1,2);
fclose(fopen('D:\sem6\PROJECT\on.csv','w'));
this is the order of my code but its giving a dlmread error (but i havent used dlmread)
Walter Roberson
Walter Roberson el 12 de Mayo de 2021
csvread() calls dlmread() with delimiter ','
You are writing to a file that is not necesarily in the same place as you are reading from, as your current directory is not necessarily D:\sem6\PROJECT
SINDU GOKULAPATI
SINDU GOKULAPATI el 16 de Mayo de 2021
Editada: SINDU GOKULAPATI el 16 de Mayo de 2021
hey walter i specified the directory but still giving error,
for context
for i=1:n
for j=1:n
angle = transpose(rnorm{i})*rnorm{j};
if angle > cosd(20) && angle <= cosd(0)
matrix = [i,j,angle];
dlmwrite('D:\sem6\PROJECT\on.csv',(matrix),'-append')
end
end
k=0;
s=dlmread('D:\sem6\PROJECT\on.csv',',',1,2);
store= histcounts(s); //need histogram for each point wrt to angle subjected to other points
fclose(fopen('D:\sem6\PROJECT\on.csv','w')); //to rewite for every 'i'
end
ps: i have attachec csv file with 50 rnorm value (intended output is 50 histograms)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Environment and Settings en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 24 de Mzo. de 2021

Editada:

el 16 de Mayo de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by