Reference to non-existent field 'data'.

P='C:\Users\Lenovo\OneDrive\Desktop\NB_veh_files';
>> S=dir(fullfile(P,'*.csv'));
>> for K=1:numel(S)
F=fullfile(P,S(k).name);
S(k).data=readmatrix(F);
end
>> figure
>> hold on
>> for k=1:1388
plot (S(k).data(:,1), S(k).data(:,4),'black','LineWidth',0.1)
end
Reference to non-existent field 'data'.
Can anyone help me how to update the code so that I don't get the error message

6 comentarios

"Can anyone help me how to update the code so that I don't get the error message"
Most likely S is empty so the loop does not iterate and the field DATA is not created.
Please show us the output of this command:
size(S)
Laxmi Bhatta
Laxmi Bhatta el 10 de Mzo. de 2023
The output is
ans =
0 1
Stephen23
Stephen23 el 10 de Mzo. de 2023
Editada: Stephen23 el 10 de Mzo. de 2023
As I wrote, variable S is empty (because no files were found by DIR at the given path), so the loop never iterates.
Please show the output of this command:
dir(P)
Laxmi Bhatta
Laxmi Bhatta el 10 de Mzo. de 2023
Hi
Actually the problem is with
>> dir(P)
'C:\Users\Lenovo\OneDrive\Desktop\NB_veh_files' not found.
>> P='/This PC/Desktop/NB_veh_files';
>> dir(P)
'/This PC/Desktop/NB_veh_files' not found.
>> P='/Desktop/NB_veh_files';
>> dir(P)
'/Desktop/NB_veh_files' not found.
The software couldn't locate the folder
Laxmi Bhatta
Laxmi Bhatta el 10 de Mzo. de 2023
Sorry to bother you
I got it thanks
Stephen23
Stephen23 el 10 de Mzo. de 2023
Editada: Stephen23 el 10 de Mzo. de 2023
"'C:\Users\Lenovo\OneDrive\Desktop\NB_veh_files' not found."
Lenovo is an odd user name. Well, in any case, clearly you need to provide a path that actually exists and that MATLAB can access (i.e. permitted by the access rights of the account that you are using).
"'/This PC/Desktop/NB_veh_files' not found."
Of course not, "This PC" does not exist on any path, it is simply an attempt by MS to confuse its users.
"P='/Desktop/NB_veh_files'"
might work, if the MATLAB current directory is in the user folder and the files are saved in that subfolder (actually saved, not just links of whatever kind). But you do not say, what is the current directory that you have selected for MATLAB to use.
Do not simply copy from the MS File Explorer address bar, because it will give you a whole lot of nonsense (ask MS if you want to know why). Everything that you see listed under "This PC" and probably "OneDrive" and most things under your user folder do not really exist as folders in your file system, they are just MS registry magic, things that look like folders but are really just made of smoke and mirrors. Sadly MS wants to "abstract" us ways from referring to actual drive locations (because hey, everything is on the cloud, right?)
Go to your C/D/whatever drive. Navigate to the folder you want. Copy that path. Use it.
You could even use MATLAB to do the navigation.

Iniciar sesión para comentar.

Respuestas (1)

VBBV
VBBV el 10 de Mzo. de 2023
Editada: VBBV el 10 de Mzo. de 2023
Use small k, in place of K , Matlab is case sensitive
P='C:\Users\Lenovo\OneDrive\Desktop\NB_veh_files';
S=dir(fullfile(P,filesep,'*.csv'));
% check with this too
for k=1:numel(S) % use small k
F=fullfile(P,S(k).name);
S(k).data=readmatrix(F);
end
figure
hold on
for k=1:1388
plot (S(k).data(:,1), S(k).data(:,4),'black','LineWidth',0.1)
end

6 comentarios

VBBV
VBBV el 10 de Mzo. de 2023
It seems you are using K as the for loop index, but when reading the filenames, you change to smallcase k
Laxmi Bhatta
Laxmi Bhatta el 10 de Mzo. de 2023
Thanks for the comment but I have used small k
still getting the same error.
but the size of S comes out as
ans =
0 1
whereas file S has data in column 1 to 5 while I want to use the column 1 and Column 4 datas to plot the graph.
your valuable comments to resolve this would be appreciated
thanks
VBBV
VBBV el 10 de Mzo. de 2023
Editada: VBBV el 10 de Mzo. de 2023
P='C:\Users\Lenovo\OneDrive\Desktop\NB_veh_files\';
% add a backslash
A backslash to the path seems missing, to navigate to exact location where your files are present
Stephen23
Stephen23 el 10 de Mzo. de 2023
Editada: Stephen23 el 10 de Mzo. de 2023
"Add a backslash in the path"
No, this is not required, because FULLFILE does that automatically. The OPs code does not require any trailing path separator character. Lets try it right now:
P = 'C:\Users\Lenovo\OneDrive\Desktop\NB_veh_files';
fullfile(P,'*.csv')
ans = 'C:\Users\Lenovo\OneDrive\Desktop\NB_veh_files/*.csv'
(the forwardslash is only because this forum runs on Linux, on Windows it would be a backslash).
@Laxmi Bhatta, As @Stephen23 suggested, please also check whether there are any files in that folder location. Also, try using filesep in the below line,
S=dir(fullfile(P,filesep,'*.csv'));
Stephen23
Stephen23 el 10 de Mzo. de 2023
Editada: Stephen23 el 10 de Mzo. de 2023
"Also, try using filesep in the below line"
Is not required, for the reason already explained here:

Iniciar sesión para comentar.

Categorías

Más información sobre File Operations en Centro de ayuda y File Exchange.

Preguntada:

el 10 de Mzo. de 2023

Editada:

el 10 de Mzo. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by