Strcat giving error

Hi all
I have just written a code which works fine for its purpose but gives error while printing answer(Error in ==> intersection at 24
str=strcat(str,',',strcat(num2str(MMH),',',num2str(MorMH)));)
can somebody explain me why it is so?
Best
fid = fopen(strcat('OUTPUT\','intersection1','.csv'), 'w');
for i = 1:length(raw)
b= raw(i,1);
if (b==-9999)
data= raw(i+2:i+21, 1:4);
y1= data (:,1);
x1= data (:,2);
x2= data (:,3);
x3= data(:,4);
yn1= transpose (y1);
xn1= transpose (x1);
xn2=transpose (x2);
xn3= transpose (x3);
P1 = InterX([xn1;yn1],[xn2;yn1]);
P2 = InterX([xn1;yn1],[xn3;yn1]);
MMH= P1(2);
MorMH=P2(2);
str=strcat(str,',',strcat(num2str(MMH),',',num2str(MorMH)));
fprintf(fid, '%s\n', str);
end
end
fclose(fid) ;
fprintf('Program Complete');

4 件のコメント

Jan
Jan 2012 年 3 月 5 日
Please format your code as explained in the "Markup help" link on this page. In addition it is always a good idea to post the complete error message.
Muhammad
Muhammad 2012 年 3 月 5 日
Thanks Jan
I tried to find the way to manage my code here but could not find it.
Walter Roberson
Walter Roberson 2012 年 3 月 5 日
http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
Muhammad
Muhammad 2012 年 3 月 6 日
Thanks Roberson.

サインインしてコメントする。

回答 (2 件)

Image Analyst
Image Analyst 2012 年 3 月 5 日

0 投票

Why don't you use sprintf() instead? I'm sure it will be much more straightforward. It's virtually identical to fprintf() which you already know how to use.

4 件のコメント

Muhammad
Muhammad 2012 年 3 月 5 日
Hi image analyst
Can you please elaborate your answer... how fprintf() may be more useful here?
Thanks
Jan
Jan 2012 年 3 月 5 日
str=sprintf( '%s,%f,%f', str , MMH, MorMH);
Muhammad
Muhammad 2012 年 3 月 6 日
If there is no fid mentioned in sptintf, how can it then write the output to fid?
Dr. Seis
Dr. Seis 2012 年 3 月 6 日
SPRINTF doesn't write out to a file... it is creating a string (in this case called "str" as you have called it above). Then you use this output (i.e., "str") to print out the result using FPRINTF, which is what you did before. This modification was suggested in order to concatenate your string/number information in a way different from (and hopefully more error free than) the way you implemented above. Search for SPRINTF in your help menu (or type "doc sprintf" in the command window) for more information.

サインインしてコメントする。

Walter Roberson
Walter Roberson 2012 年 3 月 6 日

0 投票

You never initialize str, and you never clear it: your code just keeps growing it longer and printing out the longer and longer string. You need to fix that logic problem before we can suggest what replacement you should use.

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

タグ

質問済み:

2012 年 3 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by