Problems with function num2str

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 Commenti

KSSV
KSSV il 19 Ott 2021
Why you want to convert numbers to string? What is the necessity?
I am comfortable working with string. And do not clearly know how to do the graffic
KSSV
KSSV il 19 Ott 2021
What graphic? How you are comfartable with strings? What exactly you want to do?
my variable data is the precipitation during a month. The variable size is (22x414x744 double) where is (latitud, longitud, time) I would like to see it in a figure.
Show us some of this; it doesn't make any sense at all. num2str will convert a 3D array to a 2D char() array as the following example illustrates(*)--
>> a=rand(2,3,2)
a(:,:,1) =
0.61 0.53 0.80
0.99 0.48 0.23
a(:,:,2) =
0.50 0.57 0.74
0.90 0.85 0.59
>> num2str(a)
ans =
2×67 char array
'0.61257 0.52768 0.80135 0.49809 0.57466 0.73864'
'0.98995 0.47952 0.22784 0.90085 0.84518 0.58599'
>>
where each plane is catenated to the right of the second dimension.
Unless you've somehow redefined num2str as an array and you're just seeing whatever got stored in it.
Show us what
which -all num2str
whos num2str
(*) And, just to prove it isn't something peculiar given the size of the starting double array, I tried with an array of your given size and got the same result, just bigger
>> a=rand(22,414,744);
>> whos a
Name Size Bytes Class Attributes
a 22x414x744 54210816 double
>> s=num2str(a);
>> whos s
Name Size Bytes Class Attributes
s 22x3696188 162632272 char
>>
As for plotting, you'll have to define which variable is along each dimension of the array and extract those appropriately. We have no information on that from which to add anything further.
Ah Okay I didn't know that num2string was giving me a 2D.
And for ploting as I said the dimensions are latitud,longitud and time (22,414,744) How could I do it?
dpb
dpb il 19 Ott 2021
What, precisely, do you want the plot to show -- the evolution of the precipitation at a given location versus time, the variation in preciptation against location at specific time, ... ???
There are many possibilities; unforunately, there isn't a way to create a 4D hologram builtin; you'll have to slice and dice in some fashion...
See the documentation for graphics for examples of 2D and 3D plots, surface plots, etc., etc., etc., ...
Yeah I wanted to see the evolution of precipitation at a given localization during a period of time. I've seen but there is no example to do it during time.
dpb
dpb il 19 Ott 2021
That's essentially trivial -- select which location wanted and then all planes at that location -- P(i,j,:) is that subset. You can, of course, compute what i,j are for any given latitude and longitude based on the values for them by a lookup/interpolation process.
What we don't know is what are the corresponding lat,lon,time values that correspond to the various dimensions of the 3D array -- where/how are they stored?
the variable is 22x414x744 and the dimensions are lat x lon x time
dpb
dpb il 19 Ott 2021
But the 3D array can only hold the precipitation values -- and the indices of the array are simply indices, not the actual coordinates nor times.
Need those, too...
That's all the information I got.
dpb
dpb il 19 Ott 2021
Well, then you're stuck unless the source from which you got that can tell you what they mean, they're of little value. Undoubtedly the information will be available; you've just got to go find it. There's no way to infer what those attributes about "scale_factor" and "add_offset" mean without a key.
But, what the latitude, longitude and time associated with any given point is completely unknown -- could be Timbuktu, could be middle of Pacific Ocean, we don't know and have no way to guess, even. And as for when they were taken...
I asked for this data. I got the latitud and longitud because they asked me. But I do not understand how it can be lat lon precipitation and time in 3 dimensions
dpb
dpb il 19 Ott 2021
Modificato: dpb il 19 Ott 2021
Simple -- the array contains the precipitation numbers -- but only the precipitation data.
Each element of the array for a given plane is the data for a given lat,lon and a specific time.
But, this array can't tell you the actual coordinates of each cell nor the time -- that has to be somewhere else.
You need to go back to whomever you got the data from and ask for those data to go along with what you have.
Without it, you have nothing but a whole bunch of numbers -- you can plot each coordinate intersection versus time and get a nice graph, but unless you have some way to know what the coordinates actually are, it's just a nice graph but as before -- "where?" and "when?" are unknown.
Did you download this from somewhere, maybe? If so provide the link and somebody can probably figure out where the other data are as well--it makes no sense to provide the precip data without the location and time data to go along with it.
I've got all that you said because I asked for it. The link where I got the information is:
dpb
dpb il 19 Ott 2021
OK, then what you get is dependent upon what you click on for check boxes -- the Overview tab has the following description
Data typeGridded
ProjectionRegular latitude-longitude grid
Horizontal coverageGlobal
Horizontal resolution0.1° x 0.1°; Native resolution is 9 km.
...
Temporal coverageJanuary 1950 to present
Temporal resolutionHourly
So, from that we can see that since you said your 3rd dimension is 744,
744 points / [1 point/hr] / [24 hrs/day] ==> 31 days you downloaded. Now, you have to know which month and year you checked as well and you'll know the time.
Similarly for the lat and long -- 22 x 414 with 0.1° resolution each way -- you must have set some boundaries for the NS/EW coordinates to have limited those, otherwise they would have covered the full globe. So, go back and see what those values were and you'll know the actual coordinates.
Of course, these are GRIB files and so all the skinny is buried inside them, but MATLAB doesn't have builtin routines to extract the data from them.
Probably the best bet would be to download the data you need again, but use the NetCDF format -- MATLAB does support it.
Hi, today I downloaded the same information but just one day and I notice what you just told me about the third dimension I've got 24. About the lat and lon the ones that I've marked where North 41.37 °, West 0 °, South 0 °, East 2.19 °. And I got the information in NetCDF already. My question is: How I can grafic these information to see the evolution.(Now I am just workig with data from one day)
That's still the same as I outlined above in comment @13:15 -- but now first use the netCDF functions to extract the data from the file -- that should let you get at the lat,lon,time data that should also be in the file you downloaded besides the precipitation data.
>> lookfor netCDF
nccreate - Create variable in NetCDF file.
ncdisp - Display contents of a NetCDF source in command window.
ncinfo - Return information about a NetCDF source.
ncread - Read variable data from a NetCDF source.
ncreadatt - Read attribute value from a NetCDF source.
ncwrite - Write data to NetCDF file.
ncwriteatt - Write attribute to NetCDF file.
ncwriteschema - Add NetCDF schema definitions to NetCDF file.
>>
I'd start with ncdisp and ncinfo to poke at the file content, then move on to ncread ...
dpb
dpb il 20 Ott 2021
If you've got a file that is now relatively small, how about attaching it so somebody else can look at what the actual file content is? It would be interesting to see both file formats.
I, for one, have never had occasion to use either "in anger" so don't have any real experience...
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

Accedi per commentare.

 Risposta accettata

dpb
dpb il 21 Ott 2021
Modificato: dpb il 21 Ott 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 Commenti

Wow that was impressive. That helped me a lot. Now I am gonna look for what you talked about. Thank you so much!!!
dpb
dpb il 21 Ott 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.

Accedi per commentare.

Più risposte (1)

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])

Richiesto:

il 19 Ott 2021

Commentato:

dpb
il 21 Ott 2021

Community Treasure Hunt

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

Start Hunting!

Translated by