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 次查看(过去 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++:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/186848/image.png)
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/186849/image.png)
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!