I have created folders using an array 1-6 and loaded the CSV file now I want to save data from CSV file to the folder in form of text file.. kindly help me

for k=0:6
[status, msg, msgID] = mkdir(num2str(k));
end
X = importdata('data.csv');
*the CSV file has the folowing data*
file values
0 1 2 3 4 5 6 7 8 9 10
2 11 12 13 14 15 16 17 18 19 206
1 21 22 23 24 25 26 27 28 29 30
first column = file in which 0, 2 ,1 are names of folders second column = values which contains few numbers i want to save theses numbers in form of text file into respective folders.
how to do this .. kindly help me

 Respuesta aceptada

You can use exactly the same code as I gave you in my answer to one of your previous questions. You changed the file delimiter, so all you need to do is to specify the new file delimiter in the code:
opt = {'Delimiter',',','Bufsize',pow2(14)}; % I just changed the delimiter, that is all!
[fid,msg] = fopen('data.csv','rt');
assert(fid>=3,msg)
hdr = regexp(fgetl(fid),'\S+','match');
C = textscan(fid,'%d%s%s',opt{:});
fclose(fid);
fun = @(s)sscanf(s,'%d').';
C{2} = cell2mat(cellfun(fun,C{2},'uni',0));
giving:
>> hdr % file header
hdr =
'data/data.csv'
>> C{1}
ans =
0
0
2
4
6
2
4
3
3
2
>> C{2}(:,1:10)
ans =
70 80 82 72 58 58 60 63 54 58
151 150 147 155 148 133 111 140 170 174
231 212 156 164 174 138 161 173 182 200
24 32 36 30 32 23 19 20 30 41
4 0 0 0 0 0 0 0 0 0
55 55 55 55 55 54 60 68 54 85
20 17 19 21 25 38 42 42 46 54
77 78 79 79 78 75 60 55 47 48
85 84 90 121 101 102 133 153 153 169
255 254 255 254 254 179 122 107 95 124
>> C{3}
ans =
'Training'
'Training'
'Training'
'Training'
'Training'
'Training'
'Training'
'Training'
'Training'
'Training'
>>

Más respuestas (1)

KL
KL el 2 de Nov. de 2017
Editada: KL el 4 de Nov. de 2017
Here's an example with some dummy data.
dummydata = {0, 1:9;1,11:19;2,21:29;}
for k=1:size(dummydata,1)
mkdir(num2str(dummydata{k,1}));
filename = fullfile(pwd,'/',num2str(dummydata{k,1}),['dummy_' num2str(dummydata{k,1}) '.csv']);
dlmwrite(filename,dummydata{k,2});
end

10 comentarios

i want to read data from csv file. how to read and save data from CSV??
data = csvread('data.csv');
and if your data is an array with three rows just like your example (only the values part), then you could replace dummydata{k,2} from the above example with data(k,:)
its not saving the values in required folders. the 0 row values should save in 0 folder the 2 row values into 2 folder the 1 row values into 1 folder
It was an example, you could have modified the folder and file names however you want!
Anyway, now I have made the changes you want to save it under folder names "0,1, and 2" and the filenames would be "dummy_" and the foldername. If you don't want the dummy prefix, remove it on the 4th line of the code.
and remember, the dummydata cell array is also with some example data, you have to modify it's contents, first column should be your folder names, second column should be values.
I get this error while reading the CSV file.
Error using dlmread (line 143) Mismatch between file and format string. Trouble reading 'Numeric' field from file (row number 1, field number 1) ==> PK
Please attach a sample of your file
That's not sample data! that's a screenshot of the data you have! Please attach the file using the paperclip icon.
I want to put check on first column and third column, if first column cell value is 0 and third column string is training then save second column value as text file in 0 folder, if cell value is 1 then save second column value in 2 folder so on... same process with all cells.
That is not a CSV file. Just because you gave something the file extension .csv does not magically make it into a CSV file. It is not a CSV file, and it is not even a text file! Here is a screenshot of it, opened in Notepad++:
Does that look like a CSV file to you? Does that look like your data? Here is the that I get when I unzipped it:
That is the inside of a .xlsx file, or something similar. As requested please upload an actual CSV text file.

Iniciar sesión para comentar.

Preguntada:

el 2 de Nov. de 2017

Comentada:

el 5 de Nov. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by