i have a file, filedata.mat, containing a 1x1 struct with sub-levels that contain data from vehicle channels. the file has close to 30 measured channels. channel names, channel units and channel values (against time) are stored in separate sublevels.
i want to specify which channels (i.e. which columns of data) by column number and have the code place the data in a matrix complete with channel names in row 1 and channel units in row 2.
The code i currently have is below:
clear all;
channels = [1 4 8];
source = load('filedata.mat');
N = size(channels,2);
I = size(source.Data.DataOut,1) + 2;
data = zeros(I,N);
for i = (1:1:I)
for n = (1:1:N)
if i == 1
data(i,n) = source.Data.ChannelNames{channels(n)};
elseif i == 2
data(i,n) = source.Data.ChannelUnits{channels(n)};
else
data(i,n) = source.Data.DataOut{i,channels(n)};
end
end
end
My thinking is that this will place channelnames in row 1 (i.e. when i = 1), channelunits in row 2 (when i = 2) and will then place all remaining rows of data up to i = I.
However when i run this code i get the error "Subscripted assignment dimension mismatch." on row 14 which is:
data(i,n) = source.Data.ChannelNames{channels(n)};
if anyone can suggest a solution and - perhaps more importantly - explain where ive gone wrong (so i can learn something!) i would really appreciate it.
Thanks in advance.
I assume your ChannelNames
are cell array of strings. So the statement source.Data.ChannelNames{channels(n)}
returns a string that cannot be assigned to a numeric value in data
.
If you want to select channel names and keep them in a variable, make this variable a cell array. Then
data = cell(I,N);
data(1,:) = source.Data.ChannelNames(channels);
data(2,:) = source.Data.ChannelUnits(channels);
data(3:I,:) = num2cell(source.Data.DataOut(:,channels));