Writing variables in .csv file and saving it
이전 댓글 표시
Hi,
I have a functionality that works perfectly fine. But the output has a set of Numeric value and text strings.
for ex: abc, 1, def,1
def,2, rfg, 1
How can i save the output in a .csv file? do i need to put all my output values of the code to a Matrix then use csvwrite() function?
Please suggest your inputs
답변 (1개)
Ameer Hamza
2020년 6월 6일
If you have R2019a or later, you can save the data in a cell array and use writecell()
C = {'abc', 1, 'def' 1; 'def', 2, 'rfg', 1};
writecell(C, 'test.txt')
댓글 수: 25
Ganesh Kini
2020년 6월 6일
Ameer Hamza
2020년 6월 6일
How are you getting the output? In a variable?
Ganesh Kini
2020년 6월 6일
Ameer Hamza
2020년 6월 6일
It is not clear in which format are you getting the output. Is it just printed on the command window?
Ganesh Kini
2020년 6월 6일
Ameer Hamza
2020년 6월 6일
I think instead of printing to the command window, you can print directly to the CSV file. The actual will depend on how the values are saved inside the for-loop.
Ganesh Kini
2020년 6월 6일
편집: Ganesh Kini
2020년 6월 6일
Ameer Hamza
2020년 6월 6일
The current printf statements might not be useful. The actual issue is how all the values are saved? Are they saved in some variables?
Ganesh Kini
2020년 6월 6일
Ameer Hamza
2020년 6월 6일
If you have all the values if a same filed inside one variable then you may try fprintf like this
fprintf('Place: ')
fprintf('%s,', place_values)
Ganesh Kini
2020년 6월 6일
편집: Ganesh Kini
2020년 6월 6일
Ameer Hamza
2020년 6월 6일
I meant that you can do it for each line. For example,
fprintf('Place: ')
fprintf('%f,', place_values)
fprintf('\n');
fprintf('Repeated Place: t,')
fprintf('f,', ones(1,size(place_values)))
fprintf('\n');
fprintf('Temperature: ')
fprintf('%f,', temperature_values)
fprintf('\n');
..
..
% similarly for other fields.
Ganesh Kini
2020년 6월 6일
Ameer Hamza
2020년 6월 6일
fprintf() can write to a file as well
fid = fopen('filename.csv');
fprintf(fid, 'Place: ')
fprintf(fid, '%f,', place_values)
fprintf(fid, '\n');
fprintf(fid, 'Repeated Place: t,')
fprintf(fid, 'f,', ones(1,size(place_values)))
fprintf(fid, '\n');
fprintf(fid, 'Temperature: ')
fprintf(fid, '%f,', temperature_values)
fprintf(fid, '\n');
..
..
% similarly for other fields.
fclose(fid)
Ganesh Kini
2020년 6월 6일
Ameer Hamza
2020년 6월 6일
csvwrite is used to write a numeric matrix. It is not useful here.
Ganesh Kini
2020년 6월 7일
Ameer Hamza
2020년 6월 8일
You need to specify that you want to open the file for writing
[fid1, msg1] = fopen('file.csv', 'w');
Ganesh Kini
2020년 6월 9일
Ameer Hamza
2020년 6월 9일
Try to write it seperately
[fid, msg] = fopen ( 'file1.csv' , 'wt' );
assert (fid> = 3, msg)
fprintf (fid, '% s \t' , 'Temperature');
for
for
for
...
fprintf (fid, '% 3.0f \t' , temp);
end
end
end
fprintf (fid, '\n' , temp);
for
for
for
...
fprintf (fid, '% 3.0f \t' , vnw);
end
end
end
fprintf (fid, '\n' , temp);
% same for vpw
fclose (fid);
Ganesh Kini
2020년 6월 9일
Ameer Hamza
2020년 6월 9일
Something like this
[fid, msg] = fopen ( 'file1.csv' , 'wt' );
assert (fid> = 3, msg)
labels = {'Temperature', 'Value_n', 'Value_p'};
vars = {temp, vnw, vpw}
for i=1:numel(labels)
fprintf (fid, '% s \t' , labels{i});
for
for
for
...
fprintf (fid, '% 3.0f \t' , vars{i});
end
end
end
fprintf (fid, '\n' , temp);
end
fclose (fid);
Ganesh Kini
2020년 6월 9일
편집: Ganesh Kini
2020년 6월 9일
Ganesh Kini
2020년 6월 9일
Ameer Hamza
2020년 6월 10일
편집: Ameer Hamza
2020년 6월 10일
Are all the variables calculated before this line?
vars = {temp, vnw, vpw}
or are they calculated inside for-loop?
카테고리
도움말 센터 및 File Exchange에서 Large Files and Big Data에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!