How to solve Reference to non-existent field 'folder'.

9 visualizaciones (últimos 30 días)
Skydriver
Skydriver el 3 de Nov. de 2019
Editada: Stephen23 el 3 de Nov. de 2019
I have a coding like this
folder='D:\\MultiLinierRegression\OutputRd'
fils=dir([folder '\*.txt']); % lists all the files with a .txt extension in myfolder
[~,idx]=sort({fils.name}); % index to load .txt files alphabetically
temp=importdata([fils(idx(1)).folder '\' fils(idx(1)).name]); % read A.txt
Y=temp(:,1); X=temp(:,2:end);
for ii=idx(2:end)
temp=importdata([fils(ii).folder '\' fils(ii).name]);
X =[X temp(:,2:end)];
end
But I got a message like this
Reference to non-existent field 'folder'.
Error in CalcRdAll (line 8)
temp=importdata([fils(idx(1)).folder '\' fils(idx(1)).name]); % read A.txt
Is there any one can help?

Respuesta aceptada

Stephen23
Stephen23 el 3 de Nov. de 2019
Editada: Stephen23 el 3 de Nov. de 2019
The folder field (and recursive folder searching) was added in R2016b.
As you are not using the recursive folder search you can simply do this:
D = 'D:\\MultiLinierRegression\OutputRd'
S = dir(fullfile(D,'*.txt'));
[~,idx] = sort({S.name}); % index to load .txt files alphabetically
temp = importdata(fullfile(D,S(1).name));
Y = temp(:,1);
X = temp(:,2:end);
for ii = idx(2:end)
temp = importdata(fullfile(D,S(ii).name));
X = [X,temp(:,2:end)];
end
  2 comentarios
Skydriver
Skydriver el 3 de Nov. de 2019
Thank you with your respond,
But in my coding I got the massage like this
Error using horzcat
The following error occurred converting from double to struct:
Conversion to struct from double is not possible.
Error in CalcAll (line 14)
X = [X,temp(:,2:end)];
Stephen23
Stephen23 el 3 de Nov. de 2019
Editada: Stephen23 el 3 de Nov. de 2019
Apparently your file data do not all have the same sizes. You can avoid this error by importing into a cell array or a structure, e.g.:
D = 'D:\\MultiLinierRegression\OutputRd'
S = dir(fullfile(D,'*.txt'));
for ii = 1:numel(S)
S(ii).data = importdata(fullfile(D,S(ii).name));
end
After the loop you can investigate the sizes, adjust the sizes, and concatenate if possible.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Structures en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by