How to store multiple output tables in a cell array
53 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Andre White
el 27 de Mayo de 2022
Comentada: Andre White
el 30 de Mayo de 2022
Hi everyone:
I have some output data stored in tabular format in .txt/csv files (file name format, IEA-15-240-RWT-UMaineSemi100m45mpsSNS 'file number'-'bachnumber'.out). The tables are stored in batches from 0:30:330 (30 increments). Each batch cotains 6 tables. Hence 0 contains six tables/files, numbered 1:6. I am able to store the data/file for one batch in a cell array like this:
for ii=1:6
OutputData{ii,1} = readtable(['IEA-15-240-RWT-UMaineSemi100m45mpsSNS',num2str(ii),'-0.out'],'FileType',"text",'HeaderLines',8,"ReadVariableNames",0);
OutputData{ii,1} = table2array(OutputData{ii,1});
end
However this is just for the 0 batch. I would like to formualte the code such that the tables from all batches are stored in a cell array or other appropriate storage method. I tried the following:
for ii=1:2
for jj=0:30
OutputData{jj}{ii,1} = readtable(['IEA-15-240-RWT-UMaineSemi100m43mpsJCS',num2str(ii),'-',num2str(jj)','.out'],'FileType',"text",'HeaderLines',8,"ReadVariableNames",0);
OutputData{jj}{ii,1} = table2array(OutputData{jj}{ii,1});
end
end
But rceived the folowing error:
"Unable to perform assignment because brace indexing is not supported for variables of this type."
Numerous attempts to solve this problem have failed. I would be grateful if someone could give some guidance to help me to solve it.
Thank you.
Regards,
AOAW
1 comentario
Walter Roberson
el 27 de Mayo de 2022
Did you clear OutputData? Before the loop it exists but it is not a cell.
Note that you cannot index at j=0
Respuesta aceptada
Stephen23
el 27 de Mayo de 2022
Editada: Stephen23
el 27 de Mayo de 2022
With MATLAB it is easier and more versatile to loop over indices, rather than looping over data. This makes it easy to preallocate the cell array used to store the imported data, which will resolve the error you describe. For example:
V1 = 0:30:330;
V2 = 1:6;
N1 = numel(V1);
N2 = numel(V2);
C = cell(N1,N2); % preallocate!!!!!
for k1 = 1:N1
for k2 = 1:N2
F = sprintf('IEA-15-240-RWT-UMaineSemi100m43mpsJCS%d-%d.out', V1(k1), V2(k2));
T = readtable(F,'FileType',"text",'HeaderLines',8,"ReadVariableNames",0);
C{k1,k2} = table2array(T); % why not just use READMATRIX ?
end
end
Store your data and code separately and use an absolute/relative path to access the data files, for example:
P = 'absolute or relative path to where the files are saved';
for ..
.. readtable(fullfile(P,F), ..)
end
16 comentarios
Más respuestas (1)
Image Analyst
el 27 de Mayo de 2022
I suggest you simply things by creating intermediate variables. Trying to index a 2-D cell array inside a cell of another, outer 2-D cell array like
OutputData{jj}{ii,1}
is just getting way too complicated for me to understand. Try something simpler like perhaps this:
fCounter = 1;
for f = 0 : 30 : 330 % Loop over files.
for b = 1 : 6 % Loop over 6 batches.
thisFileName = sprintf('IEA-15-240-RWT-UMaineSemi100m43mpsJCS%d-%d.out', f, b);
if ~isfile(thisFileName)
% Skip it if the file does not exist.
fprintf('File does not exist: "%s".\n', thisFileName);
continue;
end
thisTable = readtable(thisFileName,'FileType',"text",'HeaderLines',8,"ReadVariableNames",0);
OutputData{b, fCounter} = table2array(thisTable);
end
fCounter = fCounter + 1;
end
3 comentarios
Image Analyst
el 27 de Mayo de 2022
OK. You accepted an answer already so I guess you got it all figured out so I won't put any more effort towards this.
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!