Values not getting printed in csv file
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
Hi
I have a small issue in printing the values in .csv file
Code.
for
for
for
- some lines of code
fprintf("Temperature = %3.0f\n",temp);
[fid1, msg1] = fopen('file1.csv', 'w' );
fprintf(fid1, 'temperature');
fprintf(fid1, '%3.0f', temp );
fprintf(fid1, '\n');
fclose(fid1);
end
end
end
Output on the command line.
Temperature = -40
Temperature = 0
Temperature = 25
Temperature = 60
Temperature = -40
Temperature = 0
Temperature = 25
Temperature = 60
Temperature = -40
Temperature = 0
Temperature = 25
Temperature = 60
But in the CSV file i have only the last line of the output is printed.
Temperature = 60
I need to print all the values, what can be done ?
채택된 답변
"Values not getting printed in csv file"
Actually they are getting printed, your code just keep overwriting them. The problem is that your code reopens the file on every loop iteration, and each time any existing file contents are simply discarded, so in the end, only the data from the last loop iteration will be in the file.
The usual solution is to open the file before the loops, and close it after the loops:
[fid, msg] = fopen('file1.csv','wt'); % this needs to be BEFORE the loops.
for
for
for
...
fprintf(fid, 'temperature %3.0f\n',temp);
end
end
end
fclose(fid); % this needs to be AFTER the loops
Alternatively you could open the file with the 'a' (append) option, but I would not recommend this as you would anyway need to fopen and fclose the file before the loop to clear any existing file data. The above solution is simpler.
댓글 수: 9
Thank you!!
Output on the file
Temperature = -40
Temperature = 0
Temperature = 25
Temperature = 60
Temperature = -40
Temperature = 0
Temperature = 25
Temperature = 60
Temperature = -40
Temperature = 0
Temperature = 25
Temperature = 60
But i need the Output as
Temperature: -40 0 25 60 -40 0 25 60 -40 0 25 60
How can i change ?
[fid, msg] = fopen('file1.csv','wt'); % this needs to be BEFORE the loops.
fprintf(fid,'temperature');
for
for
for
...
fprintf(fid, ' %3.0f\n',temp);
end
end
end
fclose(fid); % this needs to be AFTER the loops
Ganesh Kini
2020년 6월 7일
편집: Ganesh Kini
2020년 6월 7일
Thank so much, I got the results
I need one help from you thats when i have
[fid1, msg1] = fopen ( 'file1.csv' , 'wt' );
fprintf (fid1, 'Temperature \ t' );
fprintf ( '\ n' );
fprintf (fid1, 'Value_n \ t' );
fprintf ( '\ n' );
fprintf (fid1, 'Value_p \ t' );
for
for
for
...
fprintf (fid1, '% 3.0f \ n' , temp);
fprintf (fid1, '% 1.1f \ t' , vnw);
fprintf (fid1, '% 1.1f \ t' , vpw);
end
end
end
I get the output
Temperature VNW VPW-40 1.8 -1.5 0 1.2 -1.8 25 1.2 -1.8 60 1.2 -1.5 -40 1.2 -1.8 0 1.8 -1.2 25 0.9 -1.8 60 0.9 -1.8 -40 0.9 -1.8 0 0.9 -1.8 25 0.9 -1.8 60 0.9-1.5
If you expect each of Temperature, Value_n and Value_p to be printed on their own lines (giving three lines in total), then that is not possible using your nested-loops approach. You would have to collect the data into an array (or three vectors) and fprintf them after the loops.
Or you could simply save your data in columns (which is the standard approach), in which case it is trivial to add a new line for each loop iteration. This would be by far the simplest approach.
Ganesh Kini
2020년 6월 7일
편집: Ganesh Kini
2020년 6월 7일
Sorry i could not post the entire thing, since there was net issues
I should not be getting like that
in a single line, but i should get the values in 3 different lines
Temperature 40 and so on ----------
Value_n 1.8 and so on ----------
Value_p -1.5 and so on ----------
If i need to create arrays or columns ( which i am not that sure about) how can i do it? Please let me know about that too. I am really stuck with this
How can i save the data in columns or in array? Probably in what way.
If you could give me an example. It would be great.
[fid,msg] = fopen('file1.csv','wt');
assert(fid>=3,msg)
fprintf(fid,'%s\t%s\t%s\n','Temperature','Value_n','Value_p');
for
for
for
...
fprintf(fid,'%3.0f\t%1.1f\t%1.1f\n',temp,vnw,vpw);
end
end
end
fclose(fid);
Ganesh Kini
2020년 6월 9일
편집: Ganesh Kini
2020년 6월 9일
Hi,
It does not work as expected
Output
Temperature Value_n Value_p ,-40 0.2 0.1 20 0.1 0.4 120 0.5 4.5
where as i should get the output in such a way
Temperature, -40 20 120
Value_n, 0.2 0.1 0.5
Value_p, 0.1 0.4 4.5
Could you please suggest some changes ?
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
