The number of lines on ASCII file should be same - error

1 回表示 (過去 30 日間)
R HULD
R HULD 2019 年 10 月 22 日
編集済み: R HULD 2019 年 10 月 25 日
Hi Team
I wrote this simple code to understand loading and saving
tan =randi([-1,3],2,3)
save 'testtan.dat' tan
clear
load 'testtan.dat' 'tan'
Error using load
Number of columns on line 2 of ASCII file testtan.dat must be the same as previous lines.
So. I need help understanding what it means. My matrix seems to be fair but what does it mean ' columns of ASCII file'.
Please help.
I have used importdata and other methods, they are fine but I want to understand load function in depth as i will be using it a lot.
Thank you
R

採用された回答

Cris LaPierre
Cris LaPierre 2019 年 10 月 23 日
What are you trying to do?
You can read more about save in the doc. There are also examples there to help you get started.
If you are trying to create mat files (MATLAB's way of saving variables), try the following
tan =randi([-1,3],2,3)
save('testtan','tan')
clear
load 'testtan'
If you instead are trying to save the data to a file in ascii format, try this
tan =randi([-1,3],2,3)
writematrix(tan,'testtan.dat')
clear
tan = readmatrix('testtan.dat')
  2 件のコメント
Cris LaPierre
Cris LaPierre 2019 年 10 月 23 日
編集済み: Cris LaPierre 2019 年 10 月 23 日
If you are partial to doing it the way you've shown, then you'll need to specify the format of testtan.dat. When using save, MATLAB will try to format the file contents based on the extension. Not sure what it is doing with *.dat, but forcing '-ascii' fixes the issue you are seeing.
tan =randi([-1,3],2,3)
save('testtan.dat','tan','-ascii')
clear
tan = load('testtan.dat')
The second argument in the load command (variables) is only supported for *.mat files. If you are going to use a different file extension, that argument cannot be used.
R HULD
R HULD 2019 年 10 月 25 日
編集済み: R HULD 2019 年 10 月 25 日
thank you. all those suggestions were great. especially about .mat file argument. :)

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by