Error using load Number of columns on line 2 of ASCII file

61 ビュー (過去 30 日間)
Hamza Ali
Hamza Ali 2017 年 1 月 16 日
編集済み: Hamza Ali 2017 年 1 月 17 日
Hello everybody !
I want to make a prediction by using Extreme learning Machine (ELM), but i encountered a problem when reading data from my file excel. My data are in the following form:
Column 1: contains the date format (dd / mm / yyyy hh: mm)
Column 2: contains numeric values.
Here is the code :
train_data=load(TrainingData_File);
T=train_data(:,1)';
P=train_data(:,2:size(train_data,2))';
clear train_data;
Here is the error :
Error using load
Number of columns on line 2 of ASCII file SolarData_training.xlsx must be the same as previous lines.
I use Matlab R2014 a.
Thank you so much !

採用された回答

Star Strider
Star Strider 2017 年 1 月 16 日
It is best to not use the load function with Excel files. Use xlsread to import your data. Use the datenum function to convert your date and time strings to date numbers and other formats.

その他の回答 (4 件)

Niels
Niels 2017 年 1 月 16 日
編集済み: Niels 2017 年 1 月 16 日
matlab tries to create a matrix with 2 rows. obviously your second row has more or less colums than the first row, so your matrix looks like
A =
1 0 2
3 4
such a matrix does not exist, thats why an error returns
use xlsread and the missing colums will be filled with NaNs

Walter Roberson
Walter Roberson 2017 年 1 月 16 日
I would suggest readtable() for this purpose. As you are using R2014a, the times will not be converted to datetime objects; they will either show up as excel date numbers or as strings, depending on your operating system.
If you end up with an excel date number, then if you have the Finance toolbox you can use https://www.mathworks.com/help/finance/x2mdate.html
If you do not have that toolbox, then you can add datenum() of Dec 31 1899 to the date. But you have to be careful; see https://www.mathworks.com/matlabcentral/answers/254-converting-excel-serial-number-of-time-to-hh-mm-ss-format#answer_353

Hamza Ali
Hamza Ali 2017 年 1 月 16 日
Thanks very much for your reaction, the problem is solved using the xlsread function. However, I get only one column that the numeric values, but the one containing the date is unrecoverable.
Best regards.
  3 件のコメント
per isakson
per isakson 2017 年 1 月 17 日
編集済み: per isakson 2017 年 1 月 17 日
"However, I get only one column that the numeric values, but the one containing the date is unrecoverable." &nbsp I guess, your problem is related to the difference between the default reference dates of Excel and Matlab.
MATLAB serial date number January 0, 0000
Excel for Windows January 1, 1900
Excel for the Macintosh January 2, 1904
Walter Roberson
Walter Roberson 2017 年 1 月 17 日
It is worse than that, Excel for Windows thinks Feb 29 1900 was a leap year but it was not, so the adjustment depends on whether your dates refer to dates after Feb 28 1900.

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


Hamza Ali
Hamza Ali 2017 年 1 月 17 日
編集済み: Hamza Ali 2017 年 1 月 17 日
Hello everyone !
I followed your advice literally, which led me to the following result:
%%%%%%%%%%%Load training dataset
[x,y]=xlsread(TrainingData_File);
y=datenum(y);
T = x';
P = y';
%%%%%%%%%%%Load testing dataset
[z,t]=xlsread(TestingData_File);
t = datenum(t);
TV.T=z';
TV.P=t';
Fortunately, the function is executed successfully. But are the changes on the code of the function did not impact the results obtained ? because I get a result that appears twice as follows:
TrainingTime =
2.3088
TrainingAccuracy =
57.2277
TestingTime =
0.2028
TestingAccuracy =
57.0400
TrainingTime =
2.3088
TestingTime =
0.2028
TrainingAccuracy =
57.2277
TestingAccuracy =
57.0400
Sincerely, thanks to your answers, I advanced in my works and I saved a lot of time. Thank you very much.

カテゴリ

Help Center および File ExchangeData Import from MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by