Info

この質問は閉じられています。 編集または回答するには再度開いてください。

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

1 回表示 (過去 30 日間)
Ana Corrochano
Ana Corrochano 2020 年 6 月 9 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
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 件のコメント
jessupj
jessupj 2020 年 6 月 9 日
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.

回答 (1 件)

Chidvi Modala
Chidvi Modala 2020 年 6 月 12 日
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 件のコメント
jessupj
jessupj 2020 年 6 月 12 日
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.

Community Treasure Hunt

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

Start Hunting!

Translated by