Read a 55x55 matrix with matlab

Dear all,
I would like to read in a txt file (see attached document) the 55x55 matrix using matlab.
In the txt file, the matrix appears in a block of 9x6 followed by one line (which then accounts 55 values) . And there are 55 blocks like this.
In fact, this block contains averaging kernel values for the 55 pressure levels of the sounding instrument.
How can I read this gile so I have at the end a matrix called A which has a dimension of 55x55?
Thank you for your help,
Best regards
Mélanie

回答 (1 件)

Akira Agata
Akira Agata 2021 年 3 月 4 日

0 投票

How about the following?
A = readmatrix('Averaging_kernel_MLS_v04_0N.txt');
A = reshape(A',60,[])';
A = A(:,1:55);

6 件のコメント

Mélanie Ghysels-Dubois
Mélanie Ghysels-Dubois 2021 年 3 月 4 日
Dear Akira,
Thank you for your response. It seems like it does not work: I do have the following message:
"Error using reshape
Product of known dimensions, 60, not divisible into total number
of elements, 6696.
Error in tri (line 39)
A = reshape(A',60,[])';"
The structure of the txt file containing the matrix is quite strange. In fact, the original file is the one attached here.
Mélanie Ghysels-Dubois
Mélanie Ghysels-Dubois 2021 年 3 月 4 日
I am interested in reading the 55 blocks of 55 values and to have at the end the matric A reshaped such as size(A)=55x55.
55x55 should be 3025 values. I do not understand why the function readmatrix sees 6696 values.
Mélanie Ghysels-Dubois
Mélanie Ghysels-Dubois 2021 年 3 月 4 日
Dear Akira,
I figured out that it was a mistake from my side. I was maning the matrix as another matrix used previously in the program.
Doing things properly, your suggestion works out well!
Thank you so much.
Best
Mélanie
dpb
dpb 2021 年 3 月 4 日
Alternatively,
A=reshape(cell2mat(textscan(fid,'%f',inf,'CollectOutput',1)),55,[]).';
textscan won'r return the NaN elements that does readmatrix for the short records in the input file.
Mélanie Ghysels-Dubois
Mélanie Ghysels-Dubois 2021 年 3 月 4 日
Yes, thank you dpb, Adding this line helped removing NaNs and to get the right matrix at the end.
Thanks a lot!
dpb
dpb 2021 年 3 月 4 日
Both end up the same in the end...
>> B=readmatrix('Averaging_kernel_MLS_v04_0N.txt');
>> B=reshape(B.',60,[]).'; B=B(:,1:55);
>> all(A(:)==B(:))
ans =
logical
1
>>
It's just perhaps a little cleaner w/ textscan. Too bad TMW hasn't added the ability to pass a filename as alternate input besides just a file handle so could avoid having to have the encapsulating fopen...fclose pair.

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

カテゴリ

ヘルプ センター および File ExchangeHolidays / Seasons についてさらに検索

製品

リリース

R2018b

質問済み:

2021 年 3 月 4 日

コメント済み:

dpb
2021 年 3 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by