Problems with function num2str
Mostra commenti meno recenti
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
il 19 Ott 2021
Why you want to convert numbers to string? What is the necessity?
flashpode
il 19 Ott 2021
KSSV
il 19 Ott 2021
What graphic? How you are comfartable with strings? What exactly you want to do?
flashpode
il 19 Ott 2021
dpb
il 19 Ott 2021
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.
flashpode
il 19 Ott 2021
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., ...
flashpode
il 19 Ott 2021
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?
flashpode
il 19 Ott 2021
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...
flashpode
il 19 Ott 2021
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...
flashpode
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.
flashpode
il 19 Ott 2021
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.
flashpode
il 20 Ott 2021
dpb
il 20 Ott 2021
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
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...
flashpode
il 20 Ott 2021
Risposta accettata
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]
S = string(A)
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])
Categorie
Scopri di più su NetCDF in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


