Unable to perform assignment because the left and right sides have a different number of elements.
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
AJ
el 8 de Mayo de 2020
Comentada: AJ
el 9 de Mayo de 2020
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 comentarios
Respuesta aceptada
Cris LaPierre
el 9 de Mayo de 2020
Editada: Cris LaPierre
el 9 de Mayo de 2020
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");
Más respuestas (0)
Ver también
Categorías
Más información sobre Data Type Conversion en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!