Different Size of array after each loop interation

3 ビュー (過去 30 日間)
Fabio Taccaliti
Fabio Taccaliti 2022 年 12 月 13 日
コメント済み: Voss 2022 年 12 月 14 日
I have 63 .txt file with 26 headers lines and then 19 columns of float data. I would like to read all the files and store (in row) the first 6 columns of the .txt files (x,y,z,u,v,w). Then for each .txt file I would like to append these data in a new row.
This is the code that I did but after the first j (j=1) then the script crash because the sizes of the arrays are not the same, indeed for some reasons during the first iteration it is skipping not 26 but 52 lines (so also the first 26 of numbers that I want), while in the second is (in a correct way skiiping only 26 headers line).
inpDir = "\\ftac\Folder\CD";
files = dir(fullfile(inpDir, '*.txt'));
n_file = size(files,1);
for j=1:n_file
fid = fopen(files(j).name, 'r');
data = textscan(fid, '%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f', 'HeaderLines', 26, 'Delimiter',' ');
fclose(fid);
x(j, :) = data{1}'; % x coordinate
y(j, :) = data{2}'; % y coordinate
z(j, :) = data{3}'; % y coordinate
u(j, :) = data{4}';% u velocity
v(j, :) = data{5}'; % v velocity
w(j, :) = data{6}'; % w velocity
end
How can I solve this issue? Thanks in advance

回答 (1 件)

millercommamatt
millercommamatt 2022 年 12 月 13 日
If you want the data from each file to be on their own row, you'll need to use something like a cell array or a structure since the files are not the same length.
I'm not sure why you're getting the incorrect number of header line skipped. You syntax for that is correct. Maybe check the line endings in the source files.
  2 件のコメント
Fabio Taccaliti
Fabio Taccaliti 2022 年 12 月 14 日
I also don't understand why..
All the files have the same number of headers and lines of data..
Also because already at the second iteration it fails.
Voss
Voss 2022 年 12 月 14 日
@Fabio Taccaliti: I guess you better upload some of the files here (use the button with the paperclip icon), so that people can try to figure out what's going on.

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

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

タグ

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by