Help me to make my MATLAB code smarter to run

4 views (last 30 days)
Saeed Bello
Saeed Bello on 13 Feb 2017
Edited: Saeed Bello on 15 Feb 2017
I wrote a code to read and extract some parameter from a dataset at every hour. The import data is in a text file. The data file contains measurement records every 15 minutes for a day. My code works fine if the data record is complete. However, if a measurement at a particular time is missing, the code can not run. The attached files contain function script and two datasets:CPNFC20060102.txt (complete record) and KTBFC20120229.txt (uncomplete record). Kindly help and guide me to make this code smart to detect missing period and assign NaN values to the record.
  • Entry for KTBFC 2012/02/29 00:15:00 is missing in KTBFC20120229.txt*
  • The code is to read hourly values
Filename='CPNFC20060102.txt'; % runs fine
Data = importdata(Filename);
%
%Selecting cell Line every hour
for i=1:24;
for j=1;
FreqLine{j,i}=Data{(i-1)*64+10,1};
end
end
%
FreqLine=FreqLine';
% Spliting the cell to get character
for i=1:24
for j=1;
FreqCharLn(i,:)=FreqLine{i,j};
end
end
% Picking the parameter value of interest(foF2) from the dataset
for i=1:24
FreqStrg(i,:)=FreqCharLn(i,57:59);
end
% Convert string to number
FreqStrg(FreqStrg == ' ')='0' % replace empty spaces with zero
FreQ=str2num(FreqStrg) % Covert string to number
FreQ(FreQ==000)=nan; % Parameter is obatined
  2 Comments
Stephen23
Stephen23 on 14 Feb 2017
"make my MATLAB code smarter to run"
The best thing you could do is get rid of all of those numbered variables, and learn to use indexing.
That would make your code much simpler, and therefore neater, easier to read, easier to understand, and therefore easier to write and bugfix.

Sign in to comment.

Answers (1)

Saeed Bello
Saeed Bello on 15 Feb 2017
Edited: Saeed Bello on 15 Feb 2017
@Stephen Thanks for the guide in Writing a good code.
I have re-write the code to pick a parameter from the data set. My problem is that I do not know how to make this code to be able to use the time stamp in the dataset to pick the period of interest rather than the looping.
I made the looping to move through the line for every hour.
Kindly please help.
Filename='CPNFC20060102.txt'; % runs fine
Data = importdata(Filename);
%
%Selecting cell Line every hour
for i=1:24;
for j=1;
FreqLine{j,i}=Data{(i-1)*64+10,1};
end
end
%
FreqLine=FreqLine';
% Spliting the cell to get character
for i=1:24
for j=1;
FreqCharLn(i,:)=FreqLine{i,j};
end
end
% Picking the parameter value of interest(foF2) from the dataset
for i=1:24
FreqStrg(i,:)=FreqCharLn(i,57:59);
end
% Convert string to number
FreqStrg(FreqStrg == ' ')='0' % replace empty spaces with zero
FreQ=str2num(FreqStrg) % Covert string to number
FreQ(FreQ==000)=nan; % Parameter is obatined
  2 Comments
Stephen23
Stephen23 on 15 Feb 2017
@Saeed Bello: does this really answer to your own question? Or are you asking for more help? Why did you accept your own "answer" if it is not an answer? Accepting an answer tells people that your question has been resolved, and that you do not need further help.
Saeed Bello
Saeed Bello on 15 Feb 2017
Edited: Saeed Bello on 15 Feb 2017
@Stephen Thank you. I still need more help. The code now works fine if my data is complete. What I want is to be able to use the timestamp in the dataset to extract parameter of interest.
Thank you for your help.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!