Unable to perform assignment because the left and right sides have a different number of elements.
6 次查看(过去 30 天)
显示 更早的评论
Essentially I am trying to read a file and split the date format ," 2020-05-01T20:54:57 " into file and collecing it for plotting. I'm not sure whats causing the issue. Can you please help.
k = 0;
% Rest of the lines until the end-of-the file are the temperatures
while ~(feof(fileID))
data = fgets(fileID);
splitdata = strsplit(data,'\t');
k = k + 1;
time(k) = datenum(splitdata{1},'yyyy-mm-ddTHH:MM:SS'); --> Error happens here.
temp = [];
for j = 2:length(splitdata)
temp = [temp cell2num(splitdata(j))];
end
T(k,:) = temp;
end
fclose(fileID)
2 个评论
采纳的回答
Cris LaPierre
2020-5-9
编辑:Cris LaPierre
2020-5-9
You need to specify the column. Temp is a matrix not a vector. At the end of your while loop, you assign the temps to the columns of T.
T(k,:) = temp;
Incidentally, this is overwritting your time since the colon means all columns (1:end). You probably want (2:length(splitdata)).
Also, I'd recommend reading in your times as datetimes instead of datenums.
time(k,1) = datetime(splitdata{1},'Format','yyyy-MM-dd''T''HH:mm:ss');
However, if I were really giving advice, I'd strongly recommend using readtable to read in all the temp data. It'll be much more efficient. Use your existing code for everything else you want to import.
In order to mix data types (dates and doubles), you should load the data into a table. The setup code looks scary, but it should be quicker, especially since you actually have 57,137 rows in your file (all are delimited, but most have no entries).
% Set up import options
opts = detectImportOptions("AJ_TEST_Measur_Error.txt");
opts.DataLines = 30; %
opts.VariableNames(1) = {'Date'};
opts.VariableTypes(1) = {'datetime'};
opts = setvaropts(opts, 1, "InputFormat", 'yyyy-MM-dd''T''HH:mm:ss');
% Load the temperature data. Each entry is assigned to a variable in the table (850)
data = readtable("AJ_TEST_Measur_Error.txt",opts,"ReadVariableNames",false);
% remove any row that does not have at least one number
data = rmmissing(data,"MinNumMissing",width(data));
% If desired, you can merge the 849 temperature variables
% into a single variable which will be a matrix of all the temps.
data = mergevars(data,2:width(data),"NewVariableName","Temps");
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!