No text appearing in my .txt file using fprintf

조회 수: 9 (최근 30일)
Ana Alonso
Ana Alonso 2018년 12월 12일
댓글: Star Strider 2018년 12월 13일
Hello,
I'm trying to write the following string array to a .txt file. However, no data is being written to any file.
label =
19×3 string array
"3182.77" "3211.7042" "Syl_1"
"3305.49" "3311.4764" "Syl_2"
"3321.45" "3351.382" "Syl_3"
"3417.23" "3470.1098" "Syl_4"
"3494.06" "3560.9081" "Syl_5"
I'm trying to write the file to a specific location, so I've included the appropriate directory in my file name:
wavfilecd =
"/Users/aalonso/Documents/Birdsongs/Millibrook/Cohort1/box1_l24y109/UD/L24Y109_43320.47040589_8_8_13_4_0.txt"
I'm using the following code to export my array to a text file.
fopen(wavfilecd,'w');
fprintf(wavfilecd,'%s \t %s \t %s \n',label)
fclose('all');
Right now a file is being created in the right directory, but when I open it there's nothing there. Can anyone help me populate the text file?
Best,
Ana
  댓글 수: 2
Stephen23
Stephen23 2018년 12월 13일
편집: Stephen23 2018년 12월 13일
fid = fopen(wavfilecd,'wt');
%^^^^ ^
fprintf(fid,'%s \t %s \t %s \n',label.')
% ^^^ ^^
fclose(fid);
% ^^^
Ana Alonso
Ana Alonso 2018년 12월 13일
This is perfect, thank you so much! Was totally overlooking that I need to assign fopen to a variable!

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2018년 12월 12일
I’ve never seen that particular syntax.
Try this:
fid1 = fopen(wavfilecd,'w');
fprintf(fid1,'%s \t %s \t %s \n',label')
fclose(fid1);
Also, you need to transpose ‘label’ (as I did here) to get it to print the way you want it to.
  댓글 수: 2
Ana Alonso
Ana Alonso 2018년 12월 13일
Thank you so much! It's working perfectly now!
Star Strider
Star Strider 2018년 12월 13일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by