How to put different size vectors to one matrix or mat file?

1 回表示 (過去 30 日間)
Djordje Damnjanovic
Djordje Damnjanovic 2024 年 3 月 13 日
回答済み: Djordje Damnjanovic 2024 年 3 月 14 日
Hello everybody, I need help.
After processing some signals I obtain different size vectors that I need to put in one place, and by that I mean one matrix or matfile etc. For example, results are:
d1=1 2 3 4 5 6 7 8
d2=1 2 3 4 5
d3=1 2 3
d4=1
So, I need to combine them into one matrix (or matfile) like this:
d5 =1 2 3 4 5 6 7 8
1 2 3 4 5
1 2 3
1
What I also need is to put ''d5'' to txt file with ";"' at the end of each row. like this:
1 2 3 4 5 6 7 8;
1 2 3 4 5;
1 2 3 ;
1;
Thanks a lot for all suggestions.

採用された回答

Matt J
Matt J 2024 年 3 月 14 日
編集済み: Matt J 2024 年 3 月 14 日
To save to a .mat file
save filename d1 d2 d3 d4
To retrieve them, packaged in a struct variable,
S=load('filename');
To write to a text file,
d={1:8;1:5;1:3;1}; d(:,end)={';'}
for i=1:height(d), writecell(d(i,:), 'outfile.txt', Delim=' ',WriteMode='append'); end
  1 件のコメント
Djordje Damnjanovic
Djordje Damnjanovic 2024 年 3 月 14 日
Thank You very much Matt! his was very helpfull!

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

その他の回答 (2 件)

Chunru
Chunru 2024 年 3 月 14 日
d1=[1 2 3 4 5 6 7 8];
d2=[1 2 3 4 5];
d3=[1 2 3];
d4=1;
% Use cell array
d5 ={d1; d2; d3; d4}
d5 = 4×1 cell array
{[1 2 3 4 5 6 7 8]} {[ 1 2 3 4 5]} {[ 1 2 3]} {[ 1]}
% you can save d5 as a mat file
save d5.mat d5
dir
. .. d5.mat
% write the data into a text file
fid = fopen("result.txt", "wt");
for i=1:length(d5)
fprintf(fid, " %d", d5{i});
fprintf(fid,";\n");
end
fclose(fid);
type result.txt
1 2 3 4 5 6 7 8; 1 2 3 4 5; 1 2 3; 1;
  1 件のコメント
Djordje Damnjanovic
Djordje Damnjanovic 2024 年 3 月 14 日
Thank You very much Churnu! his was very helpfull!

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


Djordje Damnjanovic
Djordje Damnjanovic 2024 年 3 月 14 日
Both answers solve my problem! Thanks a lot!

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by