Matlab reads file with different order

Hello ,
Can anyone help me as i put into matlab this txt file and it read it like that :
0.9011 0.5400
0.8480 1.4457
0.6744 1.180
thanks in advance

2 件のコメント

per isakson
per isakson 2020 年 8 月 31 日
How exactly did you read the txt-file?
chntua chntua
chntua chntua 2020 年 8 月 31 日
i use fopen to open the txt file and fscanf to print the matrix

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

 採用された回答

per isakson
per isakson 2020 年 8 月 31 日
編集済み: per isakson 2020 年 9 月 1 日

0 投票

>> fid = fopen('RQ_par.txt');
>> fs = fscanf( fid, '%f' )
fs =
0.9011
0.848
0.6744
0.54
1.4457
1.18
>> fclose(fid);
This shows how the numbers are stored in a sequence in the file on the disk. Then the reading function "decides" what are rows and what are columns. Matlab is "column major". C is row major.
Two better alternatives to read your file with R2018b are
>> dlmread('RQ_par.txt')
ans =
0.9011 0.848
0.6744 0.54
1.4457 1.18
>> rq = load('RQ_par.txt')
rq =
0.9011 0.848
0.6744 0.54
1.4457 1.18
>>

9 件のコメント

chntua chntua
chntua chntua 2020 年 9 月 1 日
if you please check my code if you can explain to me why it does not read it with the same order as in txt file
per isakson
per isakson 2020 年 9 月 1 日
"check my code" where do I find your code?
chntua chntua
chntua chntua 2020 年 9 月 1 日
here
per isakson
per isakson 2020 年 9 月 1 日
I try to say that "the same order as in txt file" is exactly what fscanf does. (You have to read the documentation carefully.) The statement
RQ = fscanf(combpar_1,'%f',[k,2]);
reads the data of the file to a vector and then reshapes that vector accourding to [k,2], i.e. puts the first three values in the first column (column major) and the following three values in the second column.
chntua chntua
chntua chntua 2020 年 9 月 1 日
yes but the order says A = fscanf(fileid,format spec , sizeA) . i want the size to be k lines which is 3 and 2 columns. how can i do that ?
per isakson
per isakson 2020 年 9 月 1 日
"want the size to be k lines which is 3 and 2 columns" Isn't that exactly what you get?
chntua chntua
chntua chntua 2020 年 9 月 1 日
yes but without changing position of elements. i want the elements of the matrix to be in the same position as in the txt file. how can i get that ?
per isakson
per isakson 2020 年 9 月 1 日
編集済み: per isakson 2020 年 9 月 1 日
Read my answer and my comments once more. I cannot explain better.
chntua chntua
chntua chntua 2020 年 9 月 1 日
okay thanks a lot for your time !

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

その他の回答 (1 件)

John D'Errico
John D'Errico 2020 年 8 月 31 日

0 投票

Must I point out the data in that file are arranged as:
data = textread('RQ_par.txt')
data =
0.9011 0.848
0.6744 0.54
1.4457 1.18
MATLAB will read in each line, one at a time. That is how the file is set up.
If you want to change the order to a different order, it is not how you read the file in, but how you perform the re-arrangment.
data = reshape(data',3,2)
data =
0.9011 0.54
0.848 1.4457
0.6744 1.18

1 件のコメント

chntua chntua
chntua chntua 2020 年 8 月 31 日
yes but the problem is that matlab does not read it as in text file. can you please check my code ? it is just 4 lines.

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

カテゴリ

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by