fprintf not printing correct string

조회 수: 4 (최근 30일)
Andrew
Andrew 2014년 7월 24일
댓글: Star Strider 2014년 7월 24일
Hello everyone,
I am trying to write a short script to make debugging some code easier but have encounter something strange. I was trying to use fprintf to write out some data to the command prompt using the following code:
str_dat = ['i1 =';'j1 =';'f1 =';'i2 =';'j2 =';'f2 =';'k1 =';'k2 ='];
dat = [i1;j1;f1;i2;j2;f2;k1;k2];
for i = 1:8
fprintf('%4s %15s \n',str_dat(i),num2str(dat(i)));
end
But for some reason the output I am getting is as follows:
i 0
j 536870912
f 2
i 1073741824
j 536870912
f 3
k -2
k 0
What happened to the '=' and the numbers in 'i2', 'j2', etc? I have never experienced this issue before.
Also, I have tried writing the data to a text file using the same code but I get the same output in the text file as well.
Thanks for your help.
Andrew
  댓글 수: 1
Andrew
Andrew 2014년 7월 24일
Also, I originally wasn't converting the dat data to a string and was just printing it as an integer but that didn't work either

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

채택된 답변

Star Strider
Star Strider 2014년 7월 24일
편집: Star Strider 2014년 7월 24일
Change your str_dat reference to str_dat(i,:):
fprintf('%4s %15s \n',str_dat(i,:),num2str(dat(i)));
  댓글 수: 2
Andrew
Andrew 2014년 7월 24일
Oh... haha, I can't believe I made this mistake, thanks!
Star Strider
Star Strider 2014년 7월 24일
My pleasure!

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

추가 답변 (1개)

Sara
Sara 2014년 7월 24일
Use:
fprintf('%4s %15s \n',str_dat(i,:),num2str(dat(i)))
You were not telling matlab to use all the columns of the array str_dat

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by