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

1 view (last 30 days)
saeeda saher
saeeda saher on 2 Nov 2017
Commented: saeeda saher on 5 Nov 2017
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

Accepted Answer

Stephen
Stephen on 5 Nov 2017
Edited: Stephen on 5 Nov 2017
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'
>>

More Answers (1)

KL
KL on 2 Nov 2017
Edited: KL on 4 Nov 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 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!