简体   繁体   中英

How to import and export in Matlab

I am trying to import a *.txt file with attributes separated by comas, always alternating an attribute (which is a number) with a value (double), for example [128 0.4325 129 0.4568] - where 128 means good data and 129 means bad data. I have 31 files (31 days in a month) with 1465-1477 lines (number of minutes in a day, but the equipament sometimes fails, so the number of rows may vary).

After reading it, I want to do some simple calculations and give different attributes depending on each case, but always a string such as '?S', 'II', 'X' (so I can't just give NaN).

What I am doing:

%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'

I get the output:

??? Scalar cell array indices required in this assignment.

What am I doing wrong?

My guess is that coluna_no_rep is either a logical or an array specifying more than one index. Both can result in errors.

Here's a minimal example for getting that error.

cell = {magic(2),'hello';[2 4 6;7 8 9],'world'};
cell{[1 2],1}(1,2) = [5 6];

which gives the error Scalar cell array indices required in this assignment. The reason for this is that MATLAB (as far as I know) cannot assign values to two different arrays at once. You could instead write,

cell = {magic(2),'hello';[2 4 6;7 8 9],'world'};
cell{1,1}(1,2) = 5;
cell{2,1}(1,2) = 6;

which runs without error. MATLAB sometimes spits out the same error when you try to access a cell using logical indexing. Try

cell = {magic(2),'hello';[2 4 6;7 8 9],'world'};
i = logical([1 1]);
cell{i,1}(1,2) = [5;6];

giving the error Scalar cell array indices required in this assignment. Again, it's because you're trying to access more than one array.

Try replacing coluna_no_rep with a single integer value corresponding to the index of interest. If you need to assign '?S' to multiple arrays, you'll probably need a for loop or to redefine Parametros.Temperatura in some way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM