How do I export data in structure array?

6 views (last 30 days)
Nery
Nery on 24 Jun 2015
Commented: Nery on 24 Jun 2015
Hi everyone, first time asking, so I hope I can express myself well.
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 answer ??? Scalar cell array indices required in this assignment.
I would really appreciate your help. Thank you
  2 Comments
Stephen23
Stephen23 on 24 Jun 2015
Edited: Stephen23 on 24 Jun 2015
A couple of tips:
  • use fullfile to concatenate filenames with paths, rather than using string concatenation.
  • do not use cell arrays when all of your data is numeric.
  • use logical indexing, which is much faster than using find, and might make your code simpler too!
Can you please upload a sample file for us to try out? You will need to press the paperclip button, then both the Choose file and Attach file buttons.
Nery
Nery on 24 Jun 2015
Hello Stephen, thank you for your help! Hereby I attach the file. I appreciate it!

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!