Reputation: 11
I have a big text file which gives me solar irradiance according to latitude and longitude with a 22 year monthly average.
These data are regional averages; not point data.
Created: March 12, 2008
Lat Lon Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Ann
-90 -180 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -179 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -178 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -177 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -176 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -175 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -174 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -173 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -172 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -171 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -170 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -169 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -168 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -167 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -166 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -165 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -164 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -163 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -162 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -161 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
Now, I want to find monthly average for lat=-90 & long=-166. How can I call them into my workspace variable?
Also, while working in matlab, will it be better to call this whole text file when we run the code or should we copy paste the whole text file in the code to make calculations faster and space efficient. Thank you for your help.
Upvotes: 0
Views: 63
Reputation: 104503
Matthew's answer is perfectly acceptable. In fact, it's the approach I'd side with. However, if you don't have access to the table
interface as it was introduced in R2013b, dlmread
may be more suitable for use. Simply skip three lines and use space as the delimiter to read in that matrix into MATLAB.
Once you read in this matrix, search for all rows where the first column has -90 and the second column has -166, index into your matrix and find the mean over each column. Assuming your text file is called data.txt
:
data = dlmread('data.txt', ' ', 3, 0);
ind = data(:,1) == -90 & data(:,2) == -166;
extract = data(ind,3:end);
mean_data = mean(extract, 1);
The first line of code reads in the text file as a numerical matrix and skips the first three lines. Spaces are used as the delimiter. Next, we find a Boolean vector which finds all rows where the first column is -90 and simultaneously the second column being -166. Once we find these rows, we subset into the data and extract only the third column and onwards as we don't want to include the latitude and longitude as part of the calculations.
Once we extract this data, we find the averages of each month individually using mean
and to find the column-wise averages, use the second parameter with the value of 1, meaning that the averages over the first dimension or the rows is what is sought.
Upvotes: 1
Reputation: 4519
I'm personally a fan of the table data type. I would do something like this:
t = readtable('punit.txt','HeaderLines',2,'Delimiter',' '); % read into table
index = t.Lat == -90 & t.Lon==-166; % create a binary indicator as to whether
% each row matches the criteria
my_data = t{index, 3:14}; % extract the data into a matlab array
mean_my_data = mean(my_data, 2); % calculate mean (2 makes it along columns)
You probably already know this, but I would put this code in a file like my_script.m
and then call the script from the matlab workspace with my_script
.
Upvotes: 2
Reputation: 291
Try Matlab Import Data button Go to Home tab-> in the Variable section-> select Import Data.
Select your text file and get whichever row or column you want to import.
Alternatively, right click name of the file in Current Folder browser and select Import Data. --> The Import Tool opens.
it is possible to create variables also in your Matlab workspace.
It works better than copy pasting manually and less time consuming.
Upvotes: 0