我正在尝试导入一个* .txt文件,其属性由逗号分隔,总是用值(double)交替属性(这是一个数字),例如[128 0.4325 129 0.4568] - 其中128表示良好的数据129表示不良数据。我有31个文件(一个月31天),1465-1477行(一天中的分钟数,但有时会失败,所以行数可能会有所不同)。
阅读之后,我想做一些简单的计算,并根据每种情况给出不同的属性,但总是一个字符串,如'?S','II','X'(所以我不能只给NaN )。
我在做什么:
%read file
caminho = 'C:\Users\nery.neto\Desktop\05';
CurrentDir = dir(fullfile(caminho,'*.wad'));
%parameters below
Temperatura=zeros(1470,31);CO=zeros(1470,31);NO=zeros(1470,31);NO2=zeros(1470,31);
NOx=zeros(1470,31);SO2=zeros(1470,31);PTS=zeros(1470,31);PM10=zeros(1470,31);
PM25=zeros(1470,31);VV=zeros(1470,31);DV=zeros(1470,31);
for i=1:size(CurrentDir,1)
[fid, errormsg] = fopen([caminho '\' CurrentDir(i,1).name],'r+');
nCols = 23;
format_aux = repmat(' %f32 %s', [1 nCols]);
format=['%s %s %s' format_aux] ; %before alternating attributes and values,there are 3 columns with strings
A1(i,:) = textscan(fid,format,'delimiter',',','headerLines', 2);
clear fid errormsg
%exemplifying with "Temperatura", but the same is done with the other parameters
Parametros.Temperatura{i,2}=[datenum(A1{i,3}) A1{i,4}];
Parametros.Temperatura{i,1}=A1{i,5};
Temperatura(1:size(A1{i,4},1),i)=[A1{i,4}];
end
%now I find repeated values, values=9999, negative values, difference between some...this part works well, but I had to create the double variables instead of working with the struct, because I couldn't either use the find command or the eval function properly
[linha_temp coluna_temp]=find(Temperatura==-9.999 | Temperatura==-9999 ...
| Temperatura==9999);
[linha_co coluna_co]=find(CO==-9.999 | CO==-9999 | CO==9999);
[linha_dv coluna_dv]=find(DV==-9.999 | DV==-9999 | DV==9999);
[linha_no coluna_no]=find(NO==-9.999 | NO==-9999 | NO==9999);
[linha_no2 coluna_no2]=find(NO2==-9.999 | NO2==-9999 | NO2==9999);
[linha_nox coluna_nox]=find(NOx==-9.999 | NOx==-9999 | NOx==9999);
[linha_pm10 coluna_pm10]=find(PM10==-9.999 | PM10==-9999 | PM10==9999);
[linha_pm25 coluna_pm25]=find(PM25==-9.999 | PM25==-9999 | PM25==9999);
[linha_pts coluna_pts]=find(PTS==-9.999 | PTS==-9999 | PTS==9999);
[linha_so2 coluna_so2]=find(SO2==-9.999 | SO2==-9999 | SO2==9999);
[linha_vv coluna_vv]=find(VV==-9.999 | VV==-9999 | VV==9999);
%In this part I try to export the data, but I just can't by this way
Parametros.Temperatura{coluna_no_rep,1}(linha_no_rep,1)='?S'
我得到了输出:
??? Scalar cell array indices required in this assignment.
我做错了什么?
答案 0 :(得分:0)
我的猜测是coluna_no_rep
是逻辑或指定多个索引的数组。两者都可能导致错误。
以下是获取该错误的最小示例。
cell = {magic(2),'hello';[2 4 6;7 8 9],'world'};
cell{[1 2],1}(1,2) = [5 6];
给出错误Scalar cell array indices required in this assignment.
原因是MATLAB(据我所知)不能一次为两个不同的数组赋值。你可以写一下,
cell = {magic(2),'hello';[2 4 6;7 8 9],'world'};
cell{1,1}(1,2) = 5;
cell{2,1}(1,2) = 6;
运行没有错误。当您尝试使用逻辑索引访问单元格时,MATLAB有时会发出相同的错误。尝试
cell = {magic(2),'hello';[2 4 6;7 8 9],'world'};
i = logical([1 1]);
cell{i,1}(1,2) = [5;6];
给出错误Scalar cell array indices required in this assignment.
同样,这是因为您尝试访问多个数组。
尝试使用与感兴趣的索引相对应的单整数值替换coluna_no_rep
。如果您需要将'?S'
分配给多个数组,则可能需要for循环或以某种方式重新定义Parametros.Temperatura
。