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
조회 수: 4 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Stephen23
2017년 11월 5일
편집: Stephen23
2017년 11월 5일
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'
>>
추가 답변 (1개)
KL
2017년 11월 2일
편집: KL
2017년 11월 4일
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
Stephen23
2017년 11월 5일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Text Data Preparation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!