write data to a .txt file

i have
matrix A=[1 2 3 4 5], matrix B=[6 7 8 9 10]
i want to create a text file with both matrices values separated by ';'
output data must be in this form in text file
1;6;
2;7;
3;8;
4;9;
5;10;
please let me know how can i do it

回答 (2 件)

Thorsten
Thorsten 2013 年 2 月 20 日

0 投票

fprintf(fid, '%d;%d;\n', [A' B'])

8 件のコメント

shanmukh
shanmukh 2013 年 2 月 20 日
i am getting an error
Error using horzcat
CAT arguments dimensions are not consistent.
Error in text_fin1 (line 69)
fprintf(f,'%d;%i64;\n',[q d]);
José-Luis
José-Luis 2013 年 2 月 20 日
fprintf(fid, '%d;%d;\n', [A B]')
Please read the documentation to understand how a function works. The documentation can be really bad sometimes (accumarray comes to mind), but for fprintf() it is rather decent.
Thorsten
Thorsten 2013 年 2 月 20 日
This works if q and d are row vectors of equal size
fprintf(f,'%d;%i64;\n',[q' d']);
José-Luis
José-Luis 2013 年 2 月 20 日
I guess you could make it foolproof:
fprintf(fid, '%d;%d;\n', [reshape(A,[],1) reshape(B,[],1)]')
shanmukh
shanmukh 2013 年 2 月 20 日
i need for column vectors
shanmukh
shanmukh 2013 年 2 月 20 日
the column vectors are not of equal size
José-Luis
José-Luis 2013 年 2 月 20 日
Then you can't do it like that. What do you want the output to be when there is only one value.
shanmukh
shanmukh 2013 年 2 月 20 日
i used this code
fprintf(f, '%i24;\n',d,'%d;\n',q);
i am getting values of q after values of p i need them beside each other.

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

José-Luis
José-Luis 2013 年 2 月 20 日

0 投票

Filling with NaN, when there are no values:
A = rand(10,1);
B = rand(15,1);
nRows = max([numel(A) numel(B)]);
your_mat = nan(nRows,2));
your_mat(1:numel(A),1) = A;
your_mat(1:numel(B),2) = B;
%...
fprintf(fid, '%d;%d;\n', your_mat')

カテゴリ

製品

タグ

質問済み:

2013 年 2 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by