how to define printf comand in matlab.

hello. I got result in a column matrix 4*1 like (1 0 0 0) or (0 1 0 0) etc. Now I have to provide output means printf as car for first case (that is (1 0 0 0 )), bike for second case. In this way I have 4 cases. how to write program for this please some one help me. thank you.

 採用された回答

KSSV
KSSV 2018 年 7 月 19 日
編集済み: KSSV 2018 年 7 月 19 日

0 投票

k = [1 0 0 0] ;
fprintf('%s %f %f %f %f\n','car',k') ;
If you want it in file:
c = [1 0 0 0] ;
b = [0 0 1 0] ;
fid = fopen('myfile.txt','w') ;
fprintf(fid,'%s %f %f %f %f\n','car',k') ;
fprintf(fid,'%s %f %f %f %f\n','bike',b') ;
fclose(fid) ;

6 件のコメント

shrisha tv
shrisha tv 2018 年 7 月 19 日
thank you very much. but every time only my 'k' is varing. no extra matrix will form. based on K only I have to decide which case out of four.
KSSV
KSSV 2018 年 7 月 19 日
How to decide?
shrisha tv
shrisha tv 2018 年 7 月 19 日
Based on answer it should tell which category it is. answer will be like (1 0 0 0 ) or (0 1 0 0) .. that is if answer (1 0 0 0 ) then car, (0 1 0 0 ) then bike, (0 0 1 0) then bus etc. at a time only one answer and one printf. thank you
KSSV
KSSV 2018 年 7 月 19 日
A(1,:) = [1 0 0 0] ;
A(2,:) = [0 1 0 0] ;
A(3,:) = [0 0 1 0] ;
V = {'car' , 'bike', 'bus'} ;
k = [0 0 1 0] ;
idx = false(3,1) ;
for i = 1:3
idx(i) = isequal(A(i,:),k) ;
end
fprintf('%s %f %f %f %f\n',V{idx},A(idx,:)') ;
shrisha tv
shrisha tv 2018 年 7 月 19 日
nothing printed sir. I am not able to debug. please explain that code then I find the solution.
shrisha tv
shrisha tv 2018 年 7 月 19 日
Ya i got answer, thank you very much sir.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2018 年 7 月 19 日

コメント済み:

2018 年 7 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by