Extracting data from cell array: Unable to perform assignment because the indices on the left side are not compatible with the size of the right side

Hi there, I have a 1*365 cell, in which it contains 365 3191*1 cells. I need to extract data (e.g., a GPS station data) from each 3191*1 cell.
By running the code (below), I always get 'Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.' Can anyone offer any suggestions? Thanks in advance.
clear all; clc;
loadf = 1;
files = dir('*.txt');
for i=1:length(files)
F = fullfile(files(i).name);
fid(i)=fopen(files(i).name);
files(i).values=textscan(fid(i), '%s','delimiter',',','HeaderLines',20,'MultipleDelimsAsOne',1);
end
for i = 1:length(files)
for v = 1:numel(files)
val(i) = files(v).values;
end
end
for k = 1:length(val)
WARK(i) = cell2mat(val{1,k}([2952:2963]));
end

Réponses (1)

Can you explain in which form do you want the output? The following show how you can extract those values and store them in a cell array
WARK = cell(size(val))
for k = 1:length(val)
WARK{k} = cell2mat(val{1,k}(2952:2963));
end

5 commentaires

Hi Ameer
Thanks so much for your reply and your code. According to your code, I just found another mistake I made in my code. I just adjusted my code to:
val = cell(size(files));
for i = 1:length(files)
for v = 1:numel(files)
val{i} = files(i).values;
end
end
Now, I have a 365*1 cell, this cell includes 365 1*1 cells. Each 1*1 cell contains m*1 cell (please see the attachments below).
This means 'WARK{k} = cell2mat(val{1,k}(2952:2963));' can not be applied as the WARK station data locates at different rows in each file. For example: in day 1, WARK station data from row 2952:2963; in day 2, WARK sation data from row 2947: 2958... etc. Much appreciated if you could provide any suggestions in this case. Thanks in advance.
How are the indices decided? Are they also stored somewhere?
Hi Ameer
Thanks for your reply. Not so sure about the indices. However, by using:
subval = cell(size(val));
for k = 1:length(val)
subval{k} = val{k,1}{1,1};
end
The new 365*1 cell is listed below. For example: 3279*1 cell is day 1 data, 3267*1 cell is day 2 data (please see the attachments below)... etc. I need to extract values (WARK) from each m*1 cell. Thanks.
Can you attach these variables in a .mat file. It will make it easier to suggest a solution.
Hi Ameer
Please find the attachments. I only attach 2 days data (in .mat files and .txt files) as example. Actually, those data are TRO file extension files, which I downloaded from Index of /CODE/2019/ (unibe.ch). I converted those .TRO files to .txt files in Matlab by:
files=dir('*.TRO')
for i=1:length(files)
filename=files(i).name;
[pathstr, name, ext] = fileparts(filename);
copyfile(filename, fullfile(pathstr, [name '.txt']))
end
Then, I use the code listed below (which you have already seen in the previous conversation) to extract WARK station data.
clear all; clc;
loadf = 1;
files = dir('*.txt');
for i=1:length(files)
F = fullfile(files(i).name);
fid(i)=fopen(files(i).name);
files(i).values=textscan(fid(i), '%s','delimiter',',','HeaderLines',20,'MultipleDelimsAsOne',1);
end
val = cell(size(files));
for i = 1:length(files)
for v = 1:numel(files)
val{i} = files(i).values;
end
end
subval = cell(size(val));
for k = 1:length(val)
subval{k} = val{k,1}{1,1};
end
% WARK = cell(size(subval));
for a = 1:length(subval)
WARK = cell2mat(subval{a,1}(3008:3019)); %This is not correct....
end
Thanks in advance.

Connectez-vous pour commenter.

Question posée :

le 4 Déc 2020

Commenté :

le 4 Déc 2020

Community Treasure Hunt

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

Start Hunting!

Translated by