MATLAB Answers

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

1 view (last 30 days)
Ana Corrochano
Ana Corrochano on 9 Jun 2020
Commented: jessupj on 12 Jun 2020
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
myFolder = ('C:\modelana\netcdf_2019\');
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
ncFilename = dir([myFolder '*.nc']);
for k = 1:length(ncFilename)
myfolder = fullfile(ncFilename(k).folder,ncFilename(k).name);
if isempty(ncFilename == 1)
ncfile =([myFolder ncFilename]);
s(k,:) = ncread(ncfile,'salinity') ;
t(k,:) = ncread(ncfile,'temp') ;
u(k,:) = ncread(ncfile,'u') ;


Show 1 older comment
Ana Corrochano
Ana Corrochano on 9 Jun 2020
Thank you for your answer. Now I'm having the following error:
Error using horzcat
The following error occurred converting from char to struct:
Conversion to struct from char is not possible.
jessupj on 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.

Sign in to comment.

Answers (1)

Chidvi Modala
Chidvi Modala on 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 Comment

jessupj on 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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!