MATLAB Answers

Problems with function num2str

8 views (last 30 days)
vicente Noguer
vicente Noguer on 19 Oct 2021
Commented: dpb on 21 Oct 2021
Hi, I've got a variable that is (22x414x744 double) and when I use num2str it the variable converts to (1x308016 double) could anybody explain to me why this happens? And as I want to graffic it the variables is (latitud, longitud, time) how could i do it couse I would like to do a graffic durint time to see how evolvs.
  21 Comments
vicente Noguer
vicente Noguer on 20 Oct 2021
I've already used ncdisp and read to save the variable of precipitation in another variable in order to work with it. But as I have never work with a variable of three dimensions is a bit complicated to me to understand. Here is the data in a zip. I spoke about precipitations but there are other variables. Thank you

Sign in to comment.

Accepted Answer

dpb
dpb on 21 Oct 2021
Edited: dpb on 21 Oct 2021
Well, having a file helps (as always)...
>> DatosInfo=ncinfo('..\Answers\DatosMarzoD1.nc') % see what's in the file...
DatosInfo =
struct with fields:
Filename: 'C:\Users\DPB\Documents\MATLAB\Answers\DatosMarzoD1.nc'
Name: '/'
Dimensions: [1×3 struct]
Variables: [1×9 struct]
Attributes: [1×2 struct]
Groups: []
Format: '64bit'
>> DatosInfo.Variables % which variables are in there????
ans =
1×9 struct array with fields:
Name
Dimensions
Size
Datatype
Attributes
ChunkSize
FillValue
DeflateLevel
Shuffle
>> DatosInfo.Variables.Name % and what are their names?
ans =
'longitude'
ans =
'latitude'
ans =
'time'
ans =
'u10'
ans =
'v10'
ans =
't2m'
ans =
'ssr'
ans =
'sp'
ans =
'tp'
>>
And, indeed, we find that we do have the corollary location and time variables as knew must have -- so let's load 'em into workspace--
>> lat=ncread('..\Answers\DatosMarzoD1.nc','latitude');
lon=ncread('..\Answers\DatosMarzoD1.nc','longitude');
t=ncread('..\Answers\DatosMarzoD1.nc','time');
tp=ncread('..\Answers\DatosMarzoD1.nc','tp');
>>
And now let's do something with them -- we'll plot every six hours as a surface plot -- will do as tiled arrangement to be compact for posting; a full figure for each would undoubtedly be more informative for real work; this is just illustrating pulling the data.
>> close,figure
tiledlayout('flow')
for h=0:6:24 % for the hours 0, 6, 12, ...
nexttile % make the next tile area ready
i=h+1; % arrays are 1-based, not 0-based
surf(lat,lon,tp(:,:,i)) % plot surf of this hour's data
ylabel('Longitude'),xlabel('Latitude')
legend("TP @ HR: "+h,'Location',"best")
end
produced
That should give you a start on using the data.
I could not find the reference time definition at the ERA5 web site so I don't know how to convert the time vector to actual time at the moment; that's an exercise you'll have to figure out.
Also, the above is the raw, packed data; you'll need to apply the scaling/offset factors to turn it into the real data.
See all the details of using the data besides just the manipulation of the files at the web site, I don't have the time to be able to pursue it in more depth, sorry.
  2 Comments
dpb
dpb on 21 Oct 2021
Glad to help; sorry don't have the time to spare at the moment to try to dig into the data formatting in depth. There are descriptions at the web site for the unpacking, that'll be a pretty straightforward multiply/add operation it appears.
I don't know why it seemed so difficult to find out what the time reference is, but I couldn't seem to locate anything about it anywhere. I'm sure I must just be overlooking it.

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 19 Oct 2021
If you want to convert a numeric array to an array of text where each element of the numeric array becomes its own element in the text array, I recommend using string.
A = [1, 2.5, -99; Inf, pi, NaN]
A = 2×3
1.0000 2.5000 -99.0000 Inf 3.1416 NaN
S = string(A)
S = 2×3 string array
"1" "2.5" "-99" "Inf" "3.14159" <missing>
You could use this in a call to the text function to display the strings on a figure.
[x, y] = meshgrid(1:3, [2 1]);
text(x(:), y(:), S(:))
axis([0 4 0 3])

Community Treasure Hunt

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

Start Hunting!