Filter löschen
Filter löschen

Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

Undefined operator '==' for input arguments of type 'struct'. netcdf files

1 Ansicht (letzte 30 Tage)
Ana Corrochano
Ana Corrochano am 9 Jun. 2020
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
I'm trying to do a loop to open netcdf files (14 files) and to extract some variables(temperature, salinity and velocity). This is what I've got so far. But I get the error:
Undefined operator '==' for input arguments of type 'struct'.
And I don't understand how to fix it. I know it must be something very simple, but I'm still quite new with matlab. Thanks for your time
clear
myFolder = ('C:\modelana\netcdf_2019\');
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
ncFilename = dir([myFolder '*.nc']);
for k = 1:length(ncFilename)
myfolder = fullfile(ncFilename(k).folder,ncFilename(k).name);
if isempty(ncFilename == 1)
continue;
else
ncfile =([myFolder ncFilename]);
s(k,:) = ncread(ncfile,'salinity') ;
t(k,:) = ncread(ncfile,'temp') ;
u(k,:) = ncread(ncfile,'u') ;
end
end
  4 Kommentare
jessupj
jessupj am 9 Jun. 2020
where it that occurring? i suspect that it doesn't like
[myFolder ncFilename]
because you're trying to concatenate a string (my Folder) with whatever kind of non-string object 'ncFilename' is. Note that 'ncFilename' is the object whose length you're iterating over in the loop. You maybe need:
[myFolder ncFilename(k).name] % or something like that.
Just a tip on some other programming: it looks like you use both 'myfolder' and 'myFolder' as different variables. I would avoid that by calling them something more distinguishable, like 'folder1' and 'folder2' so that 'errors' and 'typos' are easier to tell apart.

Antworten (1)

Chidvi Modala
Chidvi Modala am 12 Jun. 2020
In the provided code the resultant variable 'ncFilename' is of struct datatype. By doing this ([myFolder ncFilename]) operation you are trying to concatenate a struct with character array. Hence the error. To avoid the error, you can replace ([myFolder ncFilename]) with something like below
ncfile =([myFolder ncFilename(k).name]);
To get a proper ncfile value, you might need to add '\' as shown below
ncfile =([myFolder '\' ncFilename(k).name]);
To know more about 'struct' datatype you can refer to this
  1 Kommentar
jessupj
jessupj am 12 Jun. 2020
OP's 'myFolder' already includes the backslash, so the second line of code here isn't necessary. Just use the first one as we both suggested. Also, one wouldn't need need parenteses around brackets.

Diese Frage ist geschlossen.

Community Treasure Hunt

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

Start Hunting!

Translated by