Unable to perform assignment because the left and right sides have a different number of elements.
조회 수: 1 (최근 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에서 Text Files에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!