Clear Filters
Clear Filters

Unable to perform assignment because the left and right sides have a different number of elements.

4 views (last 30 days)
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 Comments
AJ
AJ on 8 May 2020
Edited: Cris LaPierre on 9 May 2020
Here is the code.. Essentially I am trying to read the attached Txt file and make a video with taking images. See the zip file.
% Select the file
[file,path] = uigetfile('C:\Users\sekincal\Documents\Inline Refill Project\TTA Troubleshooting\*.txt');
fileID = fopen([path,'\',file]);
% Read the first 28 lines - they contain no data
for i = 1:28
data = fgets(fileID);
end
% Read in the rest of the lines
data = fgets(fileID);
splitdata = strsplit(data,'\t');
% First line contains all the position values in mm --> convert to cm
for i = 3:length(splitdata)
x(i-1) = cell2num(splitdata(i))*100;
end
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');
temp = [];
for j = 2:length(splitdata)
temp = [temp cell2num(splitdata(j))];
end
T(k,:) = temp;
end
fclose(fileID)
% Changing timestamps to elapsed time in minutes
time = (time - time(1))*24*60;
% Start Plotting data
for i = 1:length(time)
plot(x,T(i,:),'.')
axis([0 240 0 350])
msg = ['Elapsed Time ',num2str(time(i)),' min, Data Row ',num2str(i)];
title(msg)
xlabel('Length (cm)')
ylabel('Temperature (^oC)')
hold on
G(i) = getframe(gcf);
hold off
end
% Saving the movie file
v = VideoWriter('TEST','Motion JPEG AVI');
v.FrameRate = 30 ;
open(v);
writeVideo(v,G);
close(v);

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 9 May 2020
Edited: Cris LaPierre on 9 May 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");
If you are not familiar with tables, this short video shows how to access data in a table.

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!